User Tools

Site Tools


datetime
snippet.juliarepl
julia> pkgchk( [ "julia" => v"1.0.2" ] )

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

snippet.julianoeval
[download only julia statements]
julia> using Dates:today
 
julia> Dates.today()                  ## Wells Future
2014-12-31

Creating a Date

snippet.juliarepl
julia> using Dates:Date

julia> Date(2004, 04, 05)             ## typeof is Date
20040405

Parsing Date From A String

Alternatively a format string can be passed in for a custom date string:

snippet.juliarepl
julia> using Dates:Date

julia> Date("31/12/2004", "dd/mm/yyyy")
20041231

julia> Date("12312004", "mm-dd-yyyy")
20041231

Date to YYYYMMDD Conversions

snippet.juliarepl
julia> using Dates

julia> const d= Date("20041231", "yyyymmdd")           ## create a date object
20041231

julia> Meta.parse( Dates.format( d, "yyyymmdd") )
20041231

Accessing Components

snippet.juliarepl
julia> using Dates

julia> const d= Date(2004, 12, 31)
20041231

julia> Dates.year(d)
2004

julia> Dates.month(d)
12

julia> Dates.day(d)
31

julia> Dates.monthname(d)
"December"

Weekday

snippet.juliarepl
julia> using Dates

julia> Dates.dayofweek( Date(2004, 12, 31) )
5

julia> Dates.dayname( Date(2004, 12, 31) )
"Friday"

Day Number Within Year

snippet.juliarepl
julia> using Dates

julia> Dates.dayofyear( Date(2004, 12, 31) )
366

Week Number Within Year

snippet.juliarepl
julia> using Dates

julia> Dates.week( Date(2004, 12, 31) )
53

Day Integer Index (Since Jan 1, 0001)

snippet.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
00001231

Subtracting Two Dates

snippet.juliarepl
julia> using Dates

julia> Date(2017, 12, 31) - Date(2004, 12, 31)
4748 days

Adding or Subtracting Days

snippet.juliarepl
julia> using Dates

julia> Date(2004, 12, 31) - Dates.Year(1) + Dates.Day(10)
20040110

Start and End of Week/Month/Quarter, Plus Some

Julia has unusuall good support for calendar calculations of many kinds.

snippet.juliarepl
julia> using Dates  ## import all

julia> Dates.tonext( x->true, Date(2004, 12, 31) )  ## function must returns day when condition is true
20050101

julia> Dates.toprev( x->true, Date(2004, 12, 31) )  ## function must returns day when condition is true
20041230

julia> const d= Dates.tonext( x-> (Dates.dayofweek(x) == Dates.Tuesday), Date(2004, 12, 31) )
20050104

julia> ( Dates.firstdayofweek(d), Dates.lastdayofweek(d) )   ## Monday to Sunday
(20050103, 20050109)

julia> ( Dates.firstdayofmonth(d), Dates.lastdayofmonth(d) )
(20050101, 20050131)

julia> ( Dates.firstdayofquarter(d), Dates.lastdayofquarter(d) )
(20050101, 20050331)

Fourth Thursday in November (Thanksgiving)

snippet.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##
20181122

All fourth Thursdays in each Summer Month

snippet.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}:
 20170622
 20170727
 20170824

Dates With Time

The DateTime object stores both date and time.

Current Date and Time

snippet.julianoeval
[download only julia statements]
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

snippet.julianoeval
[download only julia statements]
julia> time()                          ## (from Jan 1, 0000, 00:00:00:0000)
1.520633337181591e9

Rounding Date To Nearest

snippet.juliarepl
julia> using Dates

julia> const d= Date(2004, 06, 30);  ( floor(d, Dates.Month), ceil(d, Dates.Month) )
(20040601, 20040701)

Creating Date With Time

snippet.juliarepl
julia> using Dates

julia> DateTime(2004, 12, 31, 5, 30)   ## left out, seconds and milliseconds default to 0
200412-31T05:30:00

julia> const dt= DateTime(2004)              ## other defaults: Jan, 1, 00, 00, 00
200401-01T00:00:00

julia> Date( dt )                      ## convert answer from DateTime to Date
20040101

Parsing Date/Time From A String

snippet.juliarepl
julia> using Dates

julia> DateTime("20041231 173001","yyyymmdd HHMMSS")
200412-31T17:30:01

Accessing Date-Time fields

snippet.juliarepl
julia> using Dates

julia> const dt= DateTime(2004, 12, 31, 17, 30, 1)
200412-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.

snippet.juliarepl
julia> using Dates

julia> const dt= DateTime(2004, 12, 31, 17, 30, 1)
200412-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.

snippet.julianoeval
[download only julia statements]
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

snippet.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

snippet.juliarepl
julia> using Dates

julia> DateTime(2004, 12, 31, 17, 30) - Dates.Month(1) + Dates.Hour(2) - Dates.Second(3)
200411-30T19:29:57

Rounding DateTime to Nearest

snippet.juliarepl
julia> using Dates

julia> ceil(DateTime(2004, 12, 31, 17, 31, 30), Dates.Minute(15))
200412-31T17:45:00

Unix (not Julia) Epoch Time in Seconds

snippet.julianoeval
[download only julia statements]
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

Notes

References

datetime.txt · Last modified: 2018/11/22 20:48 (external edit)