Elixir's standard library offers 4 different modules for working with dates and time, each with its own struct.
-
The
Date
module. ADate
struct can be created with the~D
sigil.~D[2021-01-01]
-
The
Time
module. ATime
struct can be created with the~T
sigil.~T[12:00:00]
-
The
NaiveDateTime
module for datetimes without a timezone. ANaiveDateTime
struct can be created with the~N
sigil.~N[2021-01-01 12:00:00]
-
The
DateTime
module for datetimes with a timezone. Using this module for timezones other than UTC requires an external dependency, a timezone database.
To compare dates or times to one another, look for a compare
or diff
function in the corresponding module. Comparison operators such as ==
, >
, and <
seem to work, but they don't do a correct semantic comparison for those structs.