Locale-sensitive date and time formatting, modelled on
Intl.DateTimeFormat.
Formats dates, times, and datetimes according to locale conventions.
Accepts Date, Time, DateTime, and NaiveDateTime structs.
Delegates to Localize.DateTime, Localize.Date, and
Localize.Time for the underlying formatting.
Summary
Functions
Formats a date, time, or datetime value according to locale conventions.
Formats a date, time, or datetime value, raising on error.
Formats a date/time range according to locale conventions.
Formats a date/time range, raising on error.
Functions
@spec format( Date.t() | Time.t() | DateTime.t() | NaiveDateTime.t() | map(), Keyword.t() ) :: {:ok, String.t()} | {:error, term()}
Formats a date, time, or datetime value according to locale conventions.
Arguments
valueis aDate,Time,DateTime, orNaiveDateTimestruct, or a map with date and/or time keys.optionsis a keyword list of options.
Options
:localeis a locale identifier string or atom. The default is the current process locale.:date_styleis:full,:long,:medium, or:short. Provides a predefined date format. Cannot be combined with individual component options.:time_styleis:full,:long,:medium, or:short. Provides a predefined time format. Cannot be combined with individual component options.:weekdayis:long,:short, or:narrow.:yearis:numericor:"2-digit".:monthis:numeric,:"2-digit",:long,:short, or:narrow.:dayis:numericor:"2-digit".:houris:numericor:"2-digit".:minuteis:numericor:"2-digit".:secondis:numericor:"2-digit".:time_zoneis a time zone identifier string (for example,"America/New_York").:calendaris a calendar type atom (for example,:gregorian).
Returns
{:ok, formatted_string}on success.{:error, reason}if the value or options are invalid.
Examples
iex> Intl.DateTimeFormat.format(~D[2017-07-10], locale: :en, date_style: :full)
{:ok, "Monday, July 10, 2017"}
iex> Intl.DateTimeFormat.format(~D[2017-07-10], locale: :en, date_style: :short)
{:ok, "7/10/17"}
iex> Intl.DateTimeFormat.format(~N[2017-07-10 14:30:00], locale: :en, date_style: :medium, time_style: :short, prefer: :ascii)
{:ok, "Jul 10, 2017, 2:30 PM"}
@spec format!( Date.t() | Time.t() | DateTime.t() | NaiveDateTime.t() | map(), Keyword.t() ) :: String.t() | no_return()
Formats a date, time, or datetime value, raising on error.
Same as format/2 but returns the string directly or raises.
Arguments
valueis aDate,Time,DateTime, orNaiveDateTimestruct, or a map with date and/or time keys.optionsis a keyword list of options.
Returns
- A formatted string.
Examples
iex> Intl.DateTimeFormat.format!(~D[2017-07-10], locale: :en, date_style: :full)
"Monday, July 10, 2017"
Formats a date/time range according to locale conventions.
Arguments
fromis the start date, time, or datetime.tois the end date, time, or datetime.optionsis a keyword list of options. Accepts the same options asformat/2.
Returns
{:ok, formatted_string}on success.{:error, reason}if the values or options are invalid.
Examples
iex> Intl.DateTimeFormat.format_range(~D[2017-07-10], ~D[2017-07-15], locale: :en)
{:ok, "Jul 10 – 15, 2017"}
Formats a date/time range, raising on error.
Same as format_range/3 but returns the string directly or raises.
Arguments
fromis the start date, time, or datetime.tois the end date, time, or datetime.optionsis a keyword list of options.
Returns
- A formatted string.
Examples
iex> Intl.DateTimeFormat.format_range!(~D[2017-07-10], ~D[2017-07-15], locale: :en)
"Jul 10 – 15, 2017"