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.
Formats a date/time range into a list of typed parts.
Formats a date/time range into typed parts, raising on error.
Formats a date, time, or datetime into a list of typed parts.
Formats a date, time, or datetime into typed parts, 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.:erais:long,:short, or:narrow(for example, "Anno Domini", "AD", "A").: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".:day_periodis:long,:short, or:narrow. Renders flexible day periods such as "in the morning" or "noon".:fractional_second_digitsis1,2, or3. Renders that many fractional-second digits after the seconds field.:time_zone_nameis:short,:long,:short_offset,:long_offset,:short_generic, or:long_generic. Only applies toDateTimevalues with a time zone.:hour12is a boolean selecting a 12-hour (true) or 24-hour (false) clock for the:hourcomponent.:hour_cycleis:h11,:h12,:h23, or:h24. Takes precedence over:hour12.:numbering_systemis a CLDR numbering system name (for example,:thai). All numeric fields render in that system.: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"}
iex> Intl.DateTimeFormat.format(~D[2017-07-10], locale: :en, year: :numeric, month: :long, day: :numeric)
{:ok, "July 10, 2017"}
iex> Intl.DateTimeFormat.format(~T[14:30:00], locale: :en, hour: :numeric, minute: :numeric, hour12: false)
{:ok, "14:30"}
@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"
@spec format_range_to_parts(map(), map(), Keyword.t()) :: {:ok, [%{type: atom(), value: String.t(), source: atom()}]} | {:error, term()}
Formats a date/time range into a list of typed parts.
Modelled on the JS Intl.DateTimeFormat.formatRangeToParts(). In
addition to :type and :value, each part carries a :source
key: :start_range, :end_range, or :shared. When the range
endpoints have no practical difference, the single formatted value
carries source :shared throughout, matching JS.
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_range/3.
Returns
{:ok, parts}wherepartsis a list of%{type: atom, value: String.t(), source: atom}maps.{:error, reason}if the values or options are invalid.
Examples
iex> Intl.DateTimeFormat.format_range_to_parts(~D[2022-04-22], ~D[2022-04-25], locale: :en)
{:ok, [
%{type: :month, value: "Apr", source: :start_range},
%{type: :literal, value: " ", source: :start_range},
%{type: :day, value: "22", source: :start_range},
%{type: :literal, value: " – ", source: :shared},
%{type: :day, value: "25", source: :end_range},
%{type: :literal, value: ", ", source: :end_range},
%{type: :year, value: "2022", source: :end_range}
]}
@spec format_range_to_parts!(map(), map(), Keyword.t()) :: [%{type: atom(), value: String.t(), source: atom()}] | no_return()
Formats a date/time range into typed parts, raising on error.
Same as format_range_to_parts/3 but returns the parts 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 list of
%{type: atom, value: String.t(), source: atom}maps.
Examples
iex> Intl.DateTimeFormat.format_range_to_parts!(~D[2022-04-22], ~D[2022-04-25], locale: :en) |> length()
7
@spec format_to_parts( Date.t() | Time.t() | DateTime.t() | NaiveDateTime.t() | map(), Keyword.t() ) :: {:ok, [%{type: atom(), value: String.t()}]} | {:error, term()}
Formats a date, time, or datetime into a list of typed parts.
Modelled on the JS Intl.DateTimeFormat.formatToParts(). Each
part is a map with a :type and a :value key. Part types
include :year, :month, :day, :weekday, :era, :hour,
:minute, :second, :fractional_second, :day_period,
:time_zone_name, and :literal.
Arguments
valueis aDate,Time,DateTime, orNaiveDateTimestruct, or a map with date and/or time keys.optionsis a keyword list of options. Accepts the same options asformat/2.
Returns
{:ok, parts}wherepartsis a list of%{type: atom, value: String.t()}maps.{:error, reason}if the value or options are invalid.
Examples
iex> Intl.DateTimeFormat.format_to_parts(~D[2017-07-10], locale: :en, year: :numeric, month: :long, day: :numeric)
{:ok, [
%{type: :month, value: "July"},
%{type: :literal, value: " "},
%{type: :day, value: "10"},
%{type: :literal, value: ", "},
%{type: :year, value: "2017"}
]}
@spec format_to_parts!( Date.t() | Time.t() | DateTime.t() | NaiveDateTime.t() | map(), Keyword.t() ) :: [%{type: atom(), value: String.t()}] | no_return()
Formats a date, time, or datetime into typed parts, raising on error.
Same as format_to_parts/2 but returns the parts 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 list of
%{type: atom, value: String.t()}maps.
Examples
iex> Intl.DateTimeFormat.format_to_parts!(~D[2017-07-10], locale: :en, date_style: :short) |> length()
5