Days and Dates

Load Packages


In [1]:
using Dates

include("printmat.jl")


Out[1]:
printyellow (generic function with 1 method)

Building a Calendar


In [2]:
dNb = Date(2014,1,1):Dates.Month(1):Date(2014,12,1)    #build a monthly calendar

println("Dates and day of the week")
printmat([dNb Dates.dayofweek.(dNb)])


Dates and day of the week
2014-01-01         3
2014-02-01         6
2014-03-01         6
2014-04-01         2
2014-05-01         4
2014-06-01         7
2014-07-01         2
2014-08-01         5
2014-09-01         1
2014-10-01         3
2014-11-01         6
2014-12-01         1

Converting from Other Date Formats

Converting from yyyymmdd

Background: financial data is often downloaded as CSV files (eg. from finance.yahoo), where the date may look like 20160331. The next cell shows a simple way to create a Julia Date.


In [3]:
csvDate = [20160331;20160401]          #two dates

jlDate = Date.(string.(csvDate),"yyyymmdd")    #convert to string and then Julia Date

printmat(jlDate)


2016-03-31
2016-04-01

Converting from DateTime (Excel) to Date

Background: importing xls sheets with ExcelReaders gives DateTime, even if the sheets only contain dates (daily data, say)


In [4]:
xlsDate = [DateTime(2016,3,31);DateTime(2016,4,1)]     #to be converted

jlDate  = Date.(xlsDate)

println("\nDateTime and then converted to Date:")
printmat(xlsDate)
printmat(jlDate)


DateTime and then converted to Date:
2016-03-31T00:00:00
2016-04-01T00:00:00

2016-03-31
2016-04-01

Converting from Matlab's datenum to Date

Background: in Matlab datenum(2016,3,31) gives 736420.0. In contrast, in Julia (which follows the ISO 8601 standard), Dates.value(Date(2016,3,31)) gives 736054, which is 366 less (and it is an integer). A conversion is therefore required.


In [5]:
dNml = [736420.0;736421.0]            #to be converted, 2016-03-31;2016-04-01

jlDate = round.(Int,dNml) .- 366
jlDate = Date.(Dates.rata2datetime.(jlDate))  

println("\nmatlab datenum and correct Julia Date:")
printmat([dNml jlDate],width=12)


matlab datenum and correct Julia Date:
  736420.000  2016-03-31
  736421.000  2016-04-01

Time Arithmetics

You can add and subtract Dates from each other.


In [6]:
d1 = Date(2016,3,31)
d2 = Date(2016,4,30)

dif    = d2 - d1               #count the number of days between d2 and d1
difRel = Dates.value(dif)/Dates.daysinyear(d1)   #Dates.value() is the datenumber

println("difference between two dates: ",dif)
printlnPs("as a fraction of the year: ",difRel)


difference between two dates: 30 days
as a fraction of the year:      0.082

In [7]:
d3 = d1 + Dates.Month(1)          #one month after d1

println("d1 and one month later: ",d1," ",d3)


d1 and one month later: 2016-03-31 2016-04-30

Looking up Day of the Week and More


In [8]:
println("day of the week of date: ",d1," ",Dates.dayofweek(d1))
println("day of the year of date: ",d1," ",Dates.dayofyear(d1))

(y,m,d)= Dates.yearmonthday(d1)            #splitting up a date
println("Splitting up a date ",y," ",m," ",d)


day of the week of date: 2016-03-31 4
day of the year of date: 2016-03-31 91
Splitting up a date 2016 3 31

Printing a Date

with your own formatting (see the manual for many other formatting options)


In [9]:
println(Dates.format(d1,"d u yyyy"))
println(Dates.format(d1,"dd-mm-yyyy"))


31 Mar 2016
31-03-2016

In [ ]: