View Source Basic Usage

Some common scenarios with examples

getting-the-date-time

Getting the date/time

getting-the-current-datetime-in-utc

Getting the current datetime in UTC

iex> Timex.now
#<DateTime(2016-07-12T22:26:43Z Etc/UTC)>

iex> Timex.today
~D[2016-07-12]

getting-the-current-datetime-in-the-local-timezone

Getting the current datetime in the local timezone

iex> Timex.local
#<DateTime(2016-07-12T17:27:09-05:00 America/Chicago)>

getting-the-current-datetime-in-an-arbitrary-timezone

Getting the current datetime in an arbitrary timezone

iex> Timex.now("Europe/Copenhagen")
#<DateTime(2016-07-12T22:27:37+02:00 Europe/Copenhagen)>

construction

Construction

iex> Timex.to_date({2015, 6, 24})
~D[2015-06-24]

iex> Timex.to_datetime({{2015, 6, 24}, {4, 50, 34}}, "America/Chicago")
#<DateTime(2015-06-24T04:50:34-05:00 America/Chicago)>

iex> Timex.to_datetime({{2015, 6, 24}, {4, 50, 34}}, :local)
#<DateTime(2015-06-24T04:50:34-05:00 America/Chicago)>

parsing-date-time-strings

Parsing date/time strings

parsing-an-iso-8601-formatted-datetime-string

Parsing an ISO 8601-formatted DateTime string

# With timezone offset
iex> Timex.parse!("2015-06-24T04:50:34-05:00", "{ISO:Extended}")
#<DateTime(2015-06-24T04:50:34-05:00 Etc/GMT+05)>

# Without timezone offset
iex> Timex.parse!("2015-06-24T04:50:34Z", "{ISO:Extended:Z}")
#<DateTime(2015-06-24T04:50:34Z Etc/UTC)>

formatting-datetimes

Formatting DateTimes

formatting-a-datetime-as-an-iso-8601-string

Formatting a DateTime as an ISO 8601 string

iex> Timex.format!(Timex.to_datetime(~N[2015-06-24T00:04:09.293], "America/Chicago"), "{ISO:Extended}")
"2015-06-24T00:04:09.293-05:00"
iex> Timex.format!(Timex.to_datetime(~N[2015-06-24T00:04:09.293], "America/Chicago"), "{ISO:Extended:Z}")
"2015-06-24T05:04:13.293Z"

testing-if-one-event-occurs-in-an-interval

Testing if one event occurs in an interval

iex> use Timex
...> event = Timex.to_datetime({{2016, 6, 24}, {0, 0, 0}})
...> other_event = Timex.to_datetime({{2010, 1, 1}, {0, 0, 0}})
...> from = Timex.to_datetime({{2015, 1, 1}, {0, 0, 0}})
...> until = Timex.to_datetime({{2018, 1, 1}, {0, 0, 0}})
...> interval = Timex.Interval.new(from: from, until: until)
...> event in interval
true
...> other_event in interval
false