Calendar.DateTime.Format

Summary

httpdate(dt)

Takes a DateTime. Returns a string with the date-time in RFC 2616 format. This format is used in the HTTP protocol. Note that the date-time will always be "shifted" to UTC

iso_8601_basic(dt)

Format as ISO 8601 Basic

js_ms(date_time)

Takes datetime and returns UTC timestamp in JavaScript format. That is milliseconds since 1970 unix epoch

rfc2822(dt)

Format a DateTime as an RFC 2822 timestamp

rfc3339(dt)

Takes a DateTime. Returns a string with the time in RFC3339 (a profile of ISO 8601)

rfc3339(dt, decimal_count)

Takes a DateTime and a integer for number of decimals. Returns a string with the time in RFC3339 (a profile of ISO 8601)

rfc822(dt)

Format a DateTime as an RFC 822 timestamp

rfc850(dt)

Format a DateTime as an RFC 850 timestamp

strftime!(dt, string, lang \\ :en)

Deprecated in this module: The function has instead been moved to the Calendar.Strftime module

unix(date_time)

Unix time. Unix time is defined as seconds since 1970-01-01 00:00:00 UTC without leap seconds

unix_micro(datetime)

Like unix_time but returns a float with fractional seconds. If the usec of the DateTime is nil, the fractional seconds will be treated as 0.0 as seen in the second example below:

Functions

httpdate(dt)

Takes a DateTime. Returns a string with the date-time in RFC 2616 format. This format is used in the HTTP protocol. Note that the date-time will always be "shifted" to UTC.

Example

# The time is 6:09 in the morning in Montevideo, but 9:09 GMT/UTC.
iex> DateTime.from_erl!({{2014, 9, 6}, {6, 9, 8}}, "America/Montevideo") |> DateTime.Format.httpdate
"Sat, 06 Sep 2014 09:09:08 GMT"
iso_8601_basic(dt)

Format as ISO 8601 Basic

Examples

iex> Calendar.DateTime.from_erl!({{2014, 9, 26}, {20, 10, 20}}, "Etc/UTC",5) |> Calendar.DateTime.Format.iso_8601_basic
"20140926T201020Z"
iex> Calendar.DateTime.from_erl!({{2014, 9, 26}, {17, 10, 20}}, "America/Montevideo",5) |> Calendar.DateTime.Format.iso_8601_basic
"20140926T171020-0300"
js_ms(date_time)

Takes datetime and returns UTC timestamp in JavaScript format. That is milliseconds since 1970 unix epoch.

Examples

iex> DateTime.from_erl!({{2001,09,09},{03,46,40}}, "Europe/Copenhagen", 985085) |> DateTime.Format.js_ms
1_000_000_000_985

iex> DateTime.from_erl!({{2001,09,09},{03,46,40}}, "Europe/Copenhagen", 98508) |> DateTime.Format.js_ms
1_000_000_000_098
rfc2822(dt)

Format a DateTime as an RFC 2822 timestamp.

Examples

iex> Calendar.DateTime.from_erl!({{2010, 3, 13}, {11, 23, 03}}, "America/Los_Angeles") |> rfc2822
"Sat, 13 Mar 2010 11:23:03 -0800"
iex> Calendar.DateTime.from_erl!({{2010, 3, 13}, {11, 23, 03}}, "Etc/UTC") |> rfc2822
"Sat, 13 Mar 2010 11:23:03 +0000"
rfc3339(dt)

Takes a DateTime. Returns a string with the time in RFC3339 (a profile of ISO 8601)

Examples

Without microseconds

iex> Calendar.DateTime.from_erl!({{2014, 9, 26}, {17, 10, 20}}, "America/Montevideo") |> Calendar.DateTime.Format.rfc3339
"2014-09-26T17:10:20-03:00"

With microseconds

iex> Calendar.DateTime.from_erl!({{2014, 9, 26}, {17, 10, 20}}, "America/Montevideo",5) |> Calendar.DateTime.Format.rfc3339
"2014-09-26T17:10:20.000005-03:00"
rfc3339(dt, decimal_count)

Takes a DateTime and a integer for number of decimals. Returns a string with the time in RFC3339 (a profile of ISO 8601)

The decimal_count integer defines the number fractional second digits. The decimal_count must be between 0 and 6.

Fractional seconds are not rounded up, but rather trucated.

Examples

DateTime does not have microseconds, but 3 digits of fractional seconds requested. We assume 0 microseconds and display three zeroes.

iex> Calendar.DateTime.from_erl!({{2014, 9, 26}, {17, 10, 20}}, "America/Montevideo") |> Calendar.DateTime.Format.rfc3339(3)
"2014-09-26T17:10:20.000-03:00"

DateTime has microseconds and decimal count set to 6

iex> Calendar.DateTime.from_erl!({{2014, 9, 26}, {17, 10, 20}}, "America/Montevideo",5) |> Calendar.DateTime.Format.rfc3339(6)
"2014-09-26T17:10:20.000005-03:00"

DateTime has microseconds and decimal count set to 5

iex> Calendar.DateTime.from_erl!({{2014, 9, 26}, {17, 10, 20}}, "America/Montevideo",5) |> Calendar.DateTime.Format.rfc3339(5)
"2014-09-26T17:10:20.00000-03:00"

DateTime has microseconds and decimal count set to 0

iex> Calendar.DateTime.from_erl!({{2014, 9, 26}, {17, 10, 20}}, "America/Montevideo",5) |> Calendar.DateTime.Format.rfc3339(0)
"2014-09-26T17:10:20-03:00"
rfc822(dt)

Format a DateTime as an RFC 822 timestamp.

Note that this format is old and uses only 2 digits to denote the year!

Examples

iex> Calendar.DateTime.from_erl!({{2010, 3, 13}, {11, 23, 03}}, "America/Los_Angeles") |> rfc822
"Sat, 13 Mar 10 11:23:03 -0800"
iex> Calendar.DateTime.from_erl!({{2010, 3, 13}, {11, 23, 03}}, "Etc/UTC") |> rfc822
"Sat, 13 Mar 10 11:23:03 +0000"
rfc850(dt)

Format a DateTime as an RFC 850 timestamp.

Note that this format is old and uses only 2 digits to denote the year!

Examples

iex> Calendar.DateTime.from_erl!({{2010, 3, 13}, {11, 23, 03}}, "America/Los_Angeles") |> rfc850
"Sat, 13-Mar-10 11:23:03 PST"
strftime!(dt, string, lang \\ :en)

Deprecated in this module: The function has instead been moved to the Calendar.Strftime module.

unix(date_time)

Unix time. Unix time is defined as seconds since 1970-01-01 00:00:00 UTC without leap seconds.

Examples

iex> DateTime.from_erl!({{2001,09,09},{03,46,40}}, "Europe/Copenhagen", 55) |> DateTime.Format.unix
1_000_000_000
unix_micro(datetime)

Like unix_time but returns a float with fractional seconds. If the usec of the DateTime is nil, the fractional seconds will be treated as 0.0 as seen in the second example below:

Examples

iex> DateTime.from_erl!({{2001,09,09},{03,46,40}}, "Europe/Copenhagen", 985085) |> DateTime.Format.unix_micro
1_000_000_000.985085

iex> DateTime.from_erl!({{2001,09,09},{03,46,40}}, "Europe/Copenhagen") |> DateTime.Format.unix_micro
1_000_000_000.0