Provides localized formatting of DateTime, NaiveDateTime,
and datetime-like maps.
The primary function is to_string/2 which accepts a datetime
value and an options keyword list. Format patterns are defined
in CLDR and described in
TR35.
Predefined formats
:short— abbreviated date and time (e.g., "1/2/25, 3:04 PM").:medium— standard date and time (default).:long— includes time zone name.:full— verbose day-of-week, date, and time zone.
Custom CLDR skeleton strings and raw format patterns are also
supported via the :format option.
Summary
Functions
Formats a datetime into typed parts, mirroring ECMA-402's formatToParts.
Same as to_parts/2 but raises on error.
Formats a datetime according to a CLDR format pattern.
Same as to_string/2 but raises on error.
Functions
@spec to_parts(map(), Keyword.t()) :: {:ok, [%{type: atom(), value: String.t()}]} | {:error, Exception.t()}
Formats a datetime into typed parts, mirroring ECMA-402's formatToParts.
The parts concatenate to exactly the string to_string/2 produces with the same options. Each pattern field is tagged with its type (:year, :month, :day, :weekday, :hour, :minute, :second, :day_period, :time_zone_name, :era, :fractional_second, :literal, …). Standard formats, skeleton atoms, explicit pattern strings, and combined date+time wrappers all decompose.
Arguments
datetimeis aDateTime.t/0,NaiveDateTime.t/0, or any map with date and time keys.optionsis a keyword list of options.
Options
See to_string/2 for the supported options.
Returns
{:ok, parts}wherepartsis a list of%{type: atom(), value: String.t()}maps.{:error, exception}if the datetime cannot be formatted.
Examples
iex> Localize.DateTime.to_parts(~N[2017-07-10 14:30:00], format: :hm, locale: :en, prefer: :ascii)
{:ok,
[
%{type: :hour, value: "2"},
%{type: :literal, value: ":"},
%{type: :minute, value: "30"},
%{type: :literal, value: " "},
%{type: :day_period, value: "PM"}
]}
Same as to_parts/2 but raises on error.
Arguments
datetimeis aDateTime.t/0,NaiveDateTime.t/0, or any map with date and time keys.optionsis a keyword list of options. Seeto_parts/2.
Returns
- A list of
%{type: atom(), value: String.t()}maps.
Raises
- Raises an exception if the datetime cannot be formatted.
Examples
iex> Localize.DateTime.to_parts!(~N[2017-07-10 14:30:00], format: :hm, locale: :en, prefer: :ascii) |> length()
5
@spec to_string(map(), Keyword.t()) :: {:ok, String.t()} | {:error, Exception.t()}
Formats a datetime according to a CLDR format pattern.
Arguments
datetimeis aDateTime.t/0,NaiveDateTime.t/0, or any map with date and time keys.optionsis a keyword list of options.
Options
:formatis a standard format name (:short,:medium,:long,:full) or a format pattern string. The default is:medium. It sets the width of the date and the time together;:date_formatand:time_formatoverride each axis separately.:date_formatand:time_formatare standard format names that set the width of the date half and the time half independently, each defaulting to:format. Use them for the common "full date, short time" pairing:date_format: :full, time_format: :shortrenders "Wednesday, April 8, 2026, 12:00 PM". When:date_formatis given it also selects the wrapper width.:styleselects the CLDR pattern that joins the date and the time.:default(the default) uses the standard wrapper ("April 8, 2026, 12:00:00 PM");:atuses the locale's "at time" wrapper ("April 8, 2026 at 12:00:00 PM", de "8. April 2026 um 12:00:00"). CLDR defines the "at time" wrapper only for:fulland:long, so:atfalls back to the standard wrapper for:mediumand:short.:localeis a locale identifier. The default is:en.:number_systemis a CLDR numbering system name (for example,:thai). All numeric fields render in that system; a-u-nu-locale extension may be used instead. The default is the locale's number system.:preferselects between CLDRaltvariants. Accepts an atom or a list of atoms in priority order. Recognised values::standard/:variant(locales like en-CA publish both an ISO pattern"y-MM-dd"and a locale-variant"d/M/yy"), and:unicode/:ascii(mostly time formats — NBSP and curly quotes vs ASCII-only). Examples:prefer: :variant,prefer: [:variant, :ascii]. The default is[:standard, :unicode].
Returns
{:ok, formatted_string}on success.{:error, exception}if the datetime cannot be formatted.
Examples
iex> Localize.DateTime.to_string(~N[2017-07-10 14:30:00], locale: :en, prefer: :ascii)
{:ok, "Jul 10, 2017, 2:30:00 PM"}
iex> Localize.DateTime.to_string(~N[2017-07-10 14:30:00], format: :short, locale: :en, prefer: :ascii)
{:ok, "7/10/17, 2:30 PM"}
Same as to_string/2 but raises on error.
Arguments
datetimeis aDateTime.t/0,NaiveDateTime.t/0, or any map with date and time keys.optionsis a keyword list of options.
Options
See to_string/2 for the supported options.
Returns
A formatted string.
Raises an exception if the datetime cannot be formatted.
Examples
iex> Localize.DateTime.to_string!(~N[2017-07-10 14:30:00], locale: :en, prefer: :ascii)
"Jul 10, 2017, 2:30:00 PM"
iex> Localize.DateTime.to_string!(~N[2017-07-10 14:30:00], format: :short, locale: :en, prefer: :ascii)
"7/10/17, 2:30 PM"