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

Link to this function

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.DescriptionExamplereq. datereq. timereq. TZ
%aAbbreviated name of dayMon
%AFull name of dayMonday
%bAbbreviated month nameJan
%h(Equivalent to %b)
%BFull month nameJanuary
%jDay of the year as a decimal number (001 to 366).002
%uDay of the week as a decimal number (1 through 7). Also see %w1 for Monday
%wDay of the week as a decimal number (0 through 6). Also see %u0 for Sunday
%VWeek number (ISO 8601). (01 through 53)02 for week 2
%GYear for ISO 8601 week number (see %V). Not the same as %Y!2015
%g2 digit version of %G. Iso week-year. (00 through 99)15 for 2015
%y2 digit version of %Y. (00 through 99)15 for 2015
%YThe year in four digits. (0001 through 9999)2015
%CCentury number as two digits. 21st century will be 20.20 for year 2015
%IHour as decimal number using 12 hour clock. (01-12)07 for 19:00
%lLike %I but with single digits preceded by a space.7 for 19:00
%Pam or pm for 12 hour clock. In lower case.pm for 19:00
%pAM or PM for 12 hour clock. In upper case.PM for 19:00
%rTime in 12 hour notation. Equivalent to %I:%M:%S %p.07:25:41 PM
%RTime in 24 hour notation excluding seconds. Equivalent of %H:%M.19:25
%TTime in 24 hour notation. Equivalent of %H:%M:%S.19:25:41
%FDate in ISO 8601 format. Equivalent of %Y-%m-%d.2015-02-05
%xDate in in format for provided language05/02/2015
%cDate and time in format for provided languageWed Jan 13 11:34:10 2016
%vVMS date. Equivalent of %e-%b-%Y.5-Feb-2015
%mMonth as decimal number (01-12).01 for January
%eDay of the month as decimal number. Leading space if 1-digit.5 for 2015-02-05
%dDay of the month as decimal number. Leading zero. (01-31).05 for 2015-02-05
%HHour as decimal number using 24 hour clock (00-23).08 for 08:25
%kLike %H, but with leading space instead of leading zero.8 for 08:25
%MMinute as decimal number (00-59).04 for 19:04
%SSeconds as decimal number (00-60).02 for 19:04:02
%zHour and minute timezone offset from UTC.-0200
%ZTime 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 tickedCompatible input for conversion spec.
dateDate, DateTime, NaiveDateTime, datetime tuple, date tuple
timeTime, DateTime, NaiveDateTime, datetime tuple, time tuple
date and timeDateTime, NaiveDateTime, datetime tuple
date and time and TZDateTime
Link to this function

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"