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

js_ms(date_time)

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

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)

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

Generate a string from a DateTime formatted by a format string. Similar to strftime! known from UNIX

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"
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
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"
strftime!(dt, string, lang \\ :en)

Generate a string from a DateTime formatted by a format string. Similar to strftime! known from UNIX.

Example

iex> DateTime.from_erl!({{2014,9,6},{17,10,20}},"UTC") |> DateTime.Format.strftime! "%A %Y-%m-%e %H:%M:%S"
"Saturday 2014-09- 6 17:10:20"

iex> DateTime.from_erl!({{2014,9,6},{17,10,20}},"UTC") |> DateTime.Format.strftime! "%a %d.%m.%y"
"Sat 06.09.14"

iex> DateTime.from_erl!({{2014,9,6},{17,10,20}},"UTC") |> DateTime.Format.strftime! "%A %d/%m/%Y", :da
"lørdag 06/09/2014"

iex> DateTime.from_erl!({{2014,9,6},{17,10,20}},"UTC") |> DateTime.Format.strftime! "%A %d/%m/%Y", :es
"sábado 06/09/2014"
conversion spec. Description Example
%a Abbreviated name of day Mon
%A Full name of day Monday
%b Abbreviated month name Jan
%h (Equivalent to %b)
%B Full month name January
%j Day of the year as a decimal number (001 to 366). 002
%u Day of the week as a decimal number (1 through 7). Also see %w 1 for Monday
%w Day of the week as a decimal number (0 through 6). Also see %u 0 for Sunday
%V Week number (ISO 8601). (01 through 53) 02 for week 2
%G Year for ISO 8601 week number (see %V). Not the same as %Y! 2015
%g 2 digit version of %G. Iso week-year. (00 through 99) 15 for 2015
%y 2 digit version of %Y. (00 through 99) 15 for 2015
%Y The year in four digits. (0001 through 9999) 2015
%C Century number as two digits. 21st century will be 20. 20 for year 2015
%I Hour as decimal number using 12 hour clock. (01-12) 07 for 19:00
%l Like %I but with single digits preceded by a space. 7 for 19:00
%P am or pm for 12 hour clock. In lower case. pm for 19:00
%p AM or PM for 12 hour clock. In upper case. PM for 19:00
%r Time in 12 hour notation. Equivalent to %I:%M:%S %p. 07:25:41 PM
%R Time in 24 hour notation excluding seconds. Equivalent of %H:%M. 19:25
%T Time in 24 hour notation. Equivalent of %H:%M:%S. 19:25:41
%F Date in ISO 8601 format. Equivalent of %Y-%m-%d. 2015-02-05
%m Month as decimal number (01-12). 01 for January
%e Day of the month as decimal number. Leading space if 1-digit. 5 for 2015-02-05
%d Day of the month as decimal number. Leading zero. (01-31). 05 for 2015-02-05
%H Hour as decimal number using 24 hour clock (00-23). 08 for 08:25
%k Like %H, but with leading space instead of leading zero. 8 for 08:25
%M Minute as decimal number (00-59). 04 for 19:04
%S Seconds as decimal number (00-60). 02 for 19:04:02
%z Hour and minute timezone offset from UTC. -0200
%Z Time zone abbreviation. Sometimes depends on DST. UYST
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