Dates and Times

Elixir 1.3 added a calendar module and four new date and time related types. However, they are currently little more than data holders—look for third-party libraries to add functionality.

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.[6]

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

 iex>​ d1 = Date.new(2016, 12, 25)
 {:ok, ~D[2016-12-25]}
 iex>​ {​:ok​, d1} = Date.new(2016, 12, 25)
 {:ok, ~D[2016-12-25]}
 iex>​ d2 = ​~​D[2016-12-25]
 ~D[2016-12-25]
 iex>​ d1 == d2
 true
 iex>​ d1
 ~D[2016-12-25]
 iex>​ inspect d1, ​structs:​ false
 "%{__struct__: Date, calendar: Calendar.ISO, ...

Get Programming Elixir 1.3 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.