Cldr Dates & Times v1.3.1 Cldr.Date View Source

Provides an API for the localization and formatting of a Date struct or any map with the keys :year, :month, :day and :calendar.

Cldr.Date provides support for the built-in calendar Calendar.ISO. Use of other calendars may not produce the expected results.

CLDR provides standard format strings for Date which are reresented by the names :short, :medium, :long and :full. This allows for locale-independent formatting since each locale may define the underlying format string as appropriate.

Link to this section Summary

Functions

Formats a date according to a format string as defined in CLDR and described in TR35

Formats a date according to a format string as defined in CLDR and described in TR35

Link to this section Functions

Link to this function to_string!(date, options \\ []) View Source

Formats a date according to a format string as defined in CLDR and described in TR35

Arguments

  • date is a %Date{} struct or any map that contains the keys year, month, day and calendar

  • options is a keyword list of options for formatting. The valid options are:

Returns

  • formatted_date or

  • raises an exception.

Examples

iex> Cldr.Date.to_string! ~D[2017-07-10], format: :medium, locale: Cldr.Locale.new!("en")
"Jul 10, 2017"

iex> Cldr.Date.to_string! ~D[2017-07-10], locale: Cldr.Locale.new!("en")
"Jul 10, 2017"

iex> Cldr.Date.to_string! ~D[2017-07-10], format: :full, locale: Cldr.Locale.new!("en")
"Monday, July 10, 2017"

iex> Cldr.Date.to_string! ~D[2017-07-10], format: :short, locale: Cldr.Locale.new!("en")
"7/10/17"

iex> Cldr.Date.to_string! ~D[2017-07-10], format: :short, locale: "fr"
"10/07/2017"

iex> Cldr.Date.to_string! ~D[2017-07-10], format: :long, locale: "af"
"10 Julie 2017"
Link to this function to_string(date, options \\ []) View Source

Formats a date according to a format string as defined in CLDR and described in TR35

Returns either {:ok, formatted_string} or {:error, reason}.

  • date is a %Date{} struct or any map that contains the keys year, month, day and calendar

  • options is a keyword list of options for formatting. The valid options are:

    • format: :short | :medium | :long | :full or a format string. The default is :medium
    • locale: any locale returned by Cldr.known_locale_names(). The default is Cldr.get_current_locale()
    • number_system: a number system into which the formatted date digits should be transliterated

Examples

iex> Cldr.Date.to_string ~D[2017-07-10], format: :medium, locale: Cldr.Locale.new!("en")
{:ok, "Jul 10, 2017"}

iex> Cldr.Date.to_string ~D[2017-07-10], locale: Cldr.Locale.new!("en")
{:ok, "Jul 10, 2017"}

iex> Cldr.Date.to_string ~D[2017-07-10], format: :full, locale: Cldr.Locale.new!("en")
{:ok, "Monday, July 10, 2017"}

iex> Cldr.Date.to_string ~D[2017-07-10], format: :short, locale: Cldr.Locale.new!("en")
{:ok, "7/10/17"}

iex> Cldr.Date.to_string ~D[2017-07-10], format: :short, locale: "fr"
{:ok, "10/07/2017"}

iex> Cldr.Date.to_string ~D[2017-07-10], format: :long, locale: "af"
{:ok, "10 Julie 2017"}