Date

The Date struct contains the fields year, month, day and calendar. New dates can be built with the new/3 function or using the ~D sigil:

iex> ~D[2000-01-01]
~D[2000-01-01]

Both new/3 and sigil return a struct where the date fields can be accessed directly:

iex> date = ~D[2000-01-01]
iex> date.year
2000
iex> date.month
1

The functions on this module work with the Date struct as well as any struct that contains the same fields as the Date struct, such as NaiveDateTime and DateTime. Such functions expect Calendar.date/0 in their typespecs (instead of t/0).

Developers should avoid creating the Date structs directly and instead rely on the functions provided by this module as well as the ones in 3rd party calendar libraries.

Comparing dates

Comparisons in Elixir using ==, >, < and similar are structural and based on the Date struct fields. For proper comparison between dates, use the compare/2 function.

Using epochs

The add/2 and diff/2 functions can be used for computing dates or retrieving the amount of days betweens instants. For example, if there is an interest in computing the amount of days from the Unix epoch (1970-01-01):