User Tools

Site Tools


datetime

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

datetime [2018/12/27 13:27] (current)
Line 1: Line 1:
 +
 +~~CLOSETOC~~
 +
 +~~TOC 1-3 wide~~
 +
 +```juliarepl
 +julia> pkgchk.( [ "​julia"​ => v"​1.0.3"​ ] );
 +```
 +
 +
 +# Dates and Times
 +
 +The `Dates` module is included by default. ​ It provides immutable `Date` and `DateTime` data structures.
 +
 +
 +
 +## Dates
 +
 +The `Date` object stores only a date (without time).
 +
 +
 +### Today'​s Date
 +
 +```julianoeval
 +julia> using Dates:today
 +
 +julia> Dates.today() ​                 ## Wells Future
 +2014-12-31
 +```
 +
 +
 +### Creating a Date
 +
 +```juliarepl
 +julia> using Dates:Date
 +
 +julia> Date(2004, 04, 05)             ## typeof is Date
 +2004-04-05
 +```
 +
 +
 +### Parsing Date From A String
 +
 +Alternatively a format string can be passed in for a custom date string:
 +
 +```juliarepl
 +julia> using Dates:Date
 +
 +julia> Date("​31/​12/​2004",​ "​dd/​mm/​yyyy"​)
 +2004-12-31
 +
 +julia> Date("​12-31-2004",​ "​mm-dd-yyyy"​)
 +2004-12-31
 +```
 +
 +
 +### Date to YYYYMMDD Conversions
 +
 +```juliarepl
 +julia> using Dates
 +
 +julia> const d= Date("​20041231",​ "​yyyymmdd"​) ​          ## create a date object
 +2004-12-31
 +
 +julia> Meta.parse( Dates.format( d, "​yyyymmdd"​) )
 +20041231
 +
 +```
 +
 +
 +### Accessing Components
 +
 +```juliarepl
 +julia> using Dates
 +
 +julia> const d= Date(2004, 12, 31)
 +2004-12-31
 +
 +julia> Dates.year(d)
 +2004
 +
 +julia> Dates.month(d)
 +12
 +
 +julia> Dates.day(d)
 +31
 +
 +julia> Dates.monthname(d)
 +"​December"​
 +
 +```
 +
 +
 +### Weekday
 +
 +```juliarepl
 +julia> using Dates
 +
 +julia> Dates.dayofweek( Date(2004, 12, 31) )
 +5
 +
 +julia> Dates.dayname( Date(2004, 12, 31) )
 +"​Friday"​
 +
 +```
 +
 +
 +### Day Number Within Year 
 +
 +```juliarepl
 +julia> using Dates
 +
 +julia> Dates.dayofyear( Date(2004, 12, 31) )
 +366
 +```
 +
 +
 +### Week Number Within Year
 +
 +```juliarepl
 +julia> using Dates
 +
 +julia> Dates.week( Date(2004, 12, 31) )
 +53
 +```
 +
 +
 +### Day Integer Index (Since Jan 1, 0001)
 +
 +```juliarepl
 +julia> using Dates
 +
 +julia> const dti= Dates.value( Date(2004,​12,​31)) ​       ## 731,946 days since Jan 1, 0001
 +731946
 +
 +julia> Date(2004,​12,​31) - Dates.Day(dti) ​         ## day 0;  1=Jan 1, 0001
 +0000-12-31
 +```
 +
 +
 +### Subtracting Two Dates
 +
 +```juliarepl
 +julia> using Dates
 +
 +julia> Date(2017, 12, 31) - Date(2004, 12, 31)
 +4748 days
 +```
 +
 +
 +### Adding or Subtracting Days
 +
 +```juliarepl
 +julia> using Dates
 +
 +julia> Date(2004, 12, 31) - Dates.Year(1) + Dates.Day(10)
 +2004-01-10
 +
 +```
 +
 +
 +### Start and End of Week/​Month/​Quarter,​ Plus Some
 +
 +Julia has unusuall good support for calendar calculations of many kinds.
 +
 +```juliarepl
 +julia> using Dates  ## import all
 +
 +julia> Dates.tonext( x->true, Date(2004, 12, 31) )  ## function must returns day when condition is true
 +2005-01-01
 +
 +julia> Dates.toprev( x->true, Date(2004, 12, 31) )  ## function must returns day when condition is true
 +2004-12-30
 +
 +julia> const d= Dates.tonext( x-> (Dates.dayofweek(x) == Dates.Tuesday),​ Date(2004, 12, 31) )
 +2005-01-04
 +
 +julia> ( Dates.firstdayofweek(d),​ Dates.lastdayofweek(d) )   ## Monday to Sunday
 +(2005-01-03,​ 2005-01-09)
 +
 +julia> ( Dates.firstdayofmonth(d),​ Dates.lastdayofmonth(d) )
 +(2005-01-01,​ 2005-01-31)
 +
 +julia> ( Dates.firstdayofquarter(d),​ Dates.lastdayofquarter(d) )
 +(2005-01-01,​ 2005-03-31)
 +```
 +
 +
 +### Fourth Thursday in November (Thanksgiving)
 +
 +```juliarepl
 +julia> using Dates
 +
 +julia> Dates.tonext( Date(2018,​1,​1) ) do x
 +           ​(Dates.dayofweek(x) == Dates.Thursday) &&
 +           ​(Dates.dayofweekofmonth(x) == 4) &&
 +           ​(Dates.month(x) == Dates.November)
 +       ​end#​do##​
 +2018-11-22
 +```
 +
 +### All fourth Thursdays in each Summer Month
 +
 +```juliarepl
 +julia> using Dates
 +
 +julia> filter( Dates.Date(2017):​Day(1):​Dates.Date(2018) ) do x
 +                  (Dates.dayofweek(x) == Dates.Thu) &&
 +                  (Dates.Jun <= Dates.month(x) <= Dates.Aug) &&
 +                  (Dates.dayofweekofmonth(x) == 4)
 +       ​end#​do##​
 +3-element Array{Date,​1}:​
 + ​2017-06-22
 + ​2017-07-27
 + ​2017-08-24
 +```
 +
 +
 +## Dates With Time
 +
 +The `DateTime` object stores both date and time. 
 +
 +
 +### Current Date and Time
 +
 +```julianoeval
 +julia> using Dates
 +
 +julia> Dates.now() ​                ## not Date.now() or DateTime.now(); ​ also not Dates.today()
 +2004-12-31T18:​56:​04.868
 +```
 +
 +### Current Julia Epoch MilliSeconds
 +
 +```julianoeval
 +julia> time() ​                         ## (from Jan 1, 0000, 00:​00:​00:​0000)
 +1.520633337181591e9
 +```
 +
 +
 +## Rounding Date To Nearest
 +
 +```juliarepl
 +julia> using Dates
 +
 +julia> const d= Date(2004, 06, 30);  ( floor(d, Dates.Month),​ ceil(d, Dates.Month) )
 +(2004-06-01,​ 2004-07-01)
 +```
 +
 +
 +### Creating Date With Time
 +
 +```juliarepl
 +julia> using Dates
 +
 +julia> DateTime(2004,​ 12, 31, 5, 30)   ## left out, seconds and milliseconds default to 0
 +2004-12-31T05:​30:​00
 +
 +julia> const dt= DateTime(2004) ​             ## other defaults: Jan, 1, 00, 00, 00
 +2004-01-01T00:​00:​00
 +
 +julia> Date( dt )                      ## convert answer from DateTime to Date
 +2004-01-01
 +```
 +
 +
 +### Parsing Date/Time From A String
 +
 +```juliarepl
 +julia> using Dates
 +
 +julia> DateTime("​20041231 173001","​yyyymmdd HHMMSS"​)
 +2004-12-31T17:​30:​01
 +```
 +
 +
 +### Accessing Date-Time fields
 +
 +```juliarepl
 +julia> using Dates
 +
 +julia> const dt= DateTime(2004,​ 12, 31, 17, 30, 1)
 +2004-12-31T17:​30:​01
 +
 +julia> ​ Dates.year(dt),​ Dates.month(dt),​ Dates.day(dt) ​
 +( 2004, 12, 31 )
 +
 +julia> Dates.hour(dt)
 +17
 +
 +julia> Dates.minute(dt)
 +30
 +
 +julia> Dates.second(dt)
 +1
 +```
 +
 +
 +
 +### Converting a Date-Time object to Julia Epoch Milliseconds
 +
 +Julia has enough precision to count milliseconds from the turn of the zero millenium.
 +
 +```juliarepl
 +julia> using Dates
 +
 +julia> const dt= DateTime(2004,​ 12, 31, 17, 30, 1)
 +2004-12-31T17:​30:​01
 +
 +julia> const ep1= Dates.value( dt )                ## starts from year 0001
 +63240197401000
 +
 +julia> const ep2= Dates.datetime2epochms( dt )     ## starts from year 0000
 +63271733401000
 +
 +julia> (ep2-ep1) /​1000/​60/​60/​24 ​             ## one year distance
 +365.0
 +```
 +
 +It’s somewhat confusing and easy to get wrong.
 +
 +```julianoeval
 +julia> using Dates
 +
 +julia> Dates.epochms2datetime( Dates.value( Dates.now() ) )
 +2017-03-09T15:​14:​27.272
 +
 +julia> run(`date`)
 +Fri Dec 31 15:14:28 PST 2004
 +```
 +
 +
 +### Subtracting Two DateTimes
 +
 +```juliarepl
 +julia> using Dates
 +
 +julia> DateTime(2017,​ 12, 31, 17, 30, 00) - DateTime(2004,​ 12, 31, 17, 30)
 +410227200000 milliseconds
 +```
 +
 +
 +### Adding or Subtracting Days and Time From a DateTime
 +
 +
 +```juliarepl
 +julia> using Dates
 +
 +julia> DateTime(2004,​ 12, 31, 17, 30) - Dates.Month(1) + Dates.Hour(2) - Dates.Second(3)
 +2004-11-30T19:​29:​57
 +```
 +
 +
 +## Rounding DateTime to Nearest
 +
 +```juliarepl
 +julia> using Dates
 +
 +julia> ceil(DateTime(2004,​ 12, 31, 17, 31, 30), Dates.Minute(15))
 +2004-12-31T17:​45:​00
 +```
 +
 +
 +
 +## Unix (not Julia) Epoch Time in Seconds
 +
 +```julianoeval
 +julia> using Dates
 +
 +julia> Dates.unix2datetime(0)
 +1970-01-01T00:​00:​00
 +
 +julia> Dates.datetime2unix(Dates.now()) ​  ## a Float64, down to ms/ns resolution
 +1.1045142e9
 +```
 +
 +
 +
 +
 +# Backmatter
 +
 +## Useful Packages on Julia Repository
 +
 +* [Businessdays.jl](https://​github.com/​felipenoris/​BusinessDays.jl) (incl. financial contract settlement days).
 +
 +* [TimeZones.jl](https://​github.com/​JuliaTime/​TimeZones.jl)
 +
 +
 +## Notes
 +
 +## References
 +
  
datetime.txt · Last modified: 2018/12/27 13:27 (external edit)