Dates and Times

Elixir 1.3 added a calendar module and four new date- and time-related types. Initially, they were little more than data holders, but Elixir 1.5 started to add some functionality to them.

The Calendar module represents the rules used to manipulate dates. The only current implementation is Calendar.ISO, the ISO-8601 representation of the Gregorian calendar.[5]

The Date type holds a year, a month, a day, and a reference to the ruling calendar.

 iex>​ d1 = Date.new(2018, 12, 25)
 {:ok, ~D[2018-12-25]}
 iex>​ {​:ok​, d1} = Date.new(2018, 12, 25)
 {:ok, ~D[2018-12-25]}
 iex>​ d2 = ​~​D[2018-12-25]
 ~D[2018-12-25]
 iex>​ d1 == d2
 true
 iex>​ Date.day_of_week(d1)
 2
 iex>​ Date.add(d1, 7)
 ~D[2019-01-01]
 iex>​ inspect d1, ...

Get Programming Elixir ≥ 1.6 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.