Calendar v1.0.0 Calendar.Strftime View Source
Format different types of time representations as strings.
Link to this section Summary
Functions
Generate a string from a DateTime, NaiveDateTime, Time or Date formatted by a format string. Similar to strftime known from Unix systems.
Like strftime without the exclamation point, but returns the result without tagging it with :ok. Raises in case of errors.
Link to this section Functions
strftime(dt, string, lang \\ :en) View Source
Generate a string from a DateTime, NaiveDateTime, Time or Date formatted by a format string. Similar to strftime known from Unix systems.
Examples
iex> {{2014,9,6},{17,10,20}} |> Calendar.NaiveDateTime.from_erl! |> strftime("%A %Y-%m-%e %H:%M:%S")
{:ok, "Saturday 2014-09- 6 17:10:20"}
# Passing erlang style naive date time tuple directly
iex> {{2014,9,6},{17,10,20}} |> strftime("%A %Y-%m-%e %H:%M:%S")
{:ok, "Saturday 2014-09- 6 17:10:20"}
iex> Calendar.DateTime.from_erl!({{2014,9,6},{17,10,20}},"Etc/UTC") |> strftime("%A %Y-%m-%e %H:%M:%S")
{:ok, "Saturday 2014-09- 6 17:10:20"}
iex> Calendar.DateTime.from_erl!({{2014,9,6},{17,10,20}},"Etc/UTC") |> strftime("%a %d.%m.%y")
{:ok, "Sat 06.09.14"}
# Passing a Date struct
iex> Calendar.Date.from_erl!({2014,9,6}) |> strftime("%a %d.%m.%y")
{:ok, "Sat 06.09.14"}
# Trying to use date conversion specs and passing a Time struct results in errors
iex> Calendar.Time.from_erl!({12, 30, 59}) |> strftime("%a %d.%m.%y")
{:error, :missing_data_for_conversion_spec}
# Passing a Time and using just conversion specs suitable for time
iex> Calendar.Time.from_erl!({12, 30, 59}) |> strftime("%r")
{:ok, "12:30:59 PM"}
# Tuples in erlang datetime format will work like NaiveDateTime structs
iex> {{2014,9,6},{17,10,20}} |> strftime("%A %Y-%m-%e %H:%M:%S")
{:ok, "Saturday 2014-09- 6 17:10:20"}
conversion spec. | Description | Example | req. date | req. time | req. TZ |
---|---|---|---|---|---|
%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 | ✓ | ||
%x | Date in in format for provided language | 05/02/2015 | ✓ | ||
%c | Date and time in format for provided language | Wed Jan 13 11:34:10 2016 | ✓ | ✓ | |
%v | VMS date. Equivalent of %e-%b-%Y. | 5-Feb-2015 | ✓ | ||
%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 | ✓ | ✓ | ✓ |
The ticks in the table above tells you what input is needed for the conversion spec. The table below shows which kinds of input you can use depending on which boxes are ticked:
Which boxes are ticked | Compatible input for conversion spec. |
---|---|
date | Date , DateTime , NaiveDateTime , datetime tuple, date tuple |
time | Time , DateTime , NaiveDateTime , datetime tuple, time tuple |
date and time | DateTime , NaiveDateTime , datetime tuple |
date and time and TZ | DateTime |
strftime!(dt, string, lang \\ :en) View Source
Like strftime without the exclamation point, but returns the result without tagging it with :ok. Raises in case of errors.
Examples
# Passing NaiveDateTime struct
iex> {{2014,9,6},{17,10,20}} |> Calendar.NaiveDateTime.from_erl! |> strftime!("%A %Y-%m-%e %H:%M:%S")
"Saturday 2014-09- 6 17:10:20"
# Passing naive date time tuple
iex> {{2014,9,6},{17,10,20}} |> strftime!("%A %Y-%m-%e %H:%M:%S")
"Saturday 2014-09- 6 17:10:20"
# Passing Date struct
iex> {2014,9,6} |> Calendar.Date.from_erl! |> strftime!("%A %Y-%m-%e")
"Saturday 2014-09- 6"
# Passing Time struct
iex> {17,10,20} |> Calendar.Time.from_erl! |> strftime!("%H:%M:%S")
"17:10:20"