Intl.DateTimeFormat
A library for formatting dates in a language sensitve way. This module binds to Intl.DateTimeFormat.
Create
type DateTimeFormat
A DateTimeFormat object, for formatting dates in a language sensitive way.
fromLocale
:
Intl.Locale.Locale -> Intl.DateTimeFormat.DateTimeFormat
Create a DateTimeFormat using rules from the specified language
format (fromLocale "pt-BR") christmas
-- "25/12/2016"
fromOptions
:
Intl.DateTimeFormat.Options -> Intl.DateTimeFormat.DateTimeFormat
Create a DateTimeFormat using rules from the language and other options.
let
formatTime = fromOptions
{ defaults |
, locale = Locale.de
, hour = NumericNumber
, minute = TwoDigitNumber
}
|> format
in
formatTime elevenPM -- 23:00
Formatting Dates
format
:
Intl.DateTimeFormat.DateTimeFormat -> Date.Date -> String
Format a Date according to the rules of the DateTimeFormat.
format (fromLocale Locale.en) Date.now
Support and options
Not all environments will support all languages and options. These functions help determine what is supported, and what options a particular DateTimeFormat will use.
type alias Options =
{ locale : Intl.Locale.Locale
, timeZone : Maybe.Maybe Intl.TimeZone.TimeZone
, hour12 : Maybe.Maybe Bool
, weekday : Intl.DateTimeFormat.NameStyle
, era : Intl.DateTimeFormat.NameStyle
, year : Intl.DateTimeFormat.NumberStyle
, month : Intl.DateTimeFormat.MonthStyle
, day : Intl.DateTimeFormat.NumberStyle
, hour : Intl.DateTimeFormat.NumberStyle
, minute : Intl.DateTimeFormat.NumberStyle
, second : Intl.DateTimeFormat.NumberStyle
, timeZoneName : Intl.DateTimeFormat.TimeZoneStyle
}
An Options record, containing the possible settings for a DateTimeFormat object.
type NameStyle
= NarrowName
| ShortName
| LongName
| OmitName
Style options for the date parts that can be names.
type NumberStyle
= NumericNumber
| TwoDigitNumber
| OmitNumber
Style options for the date parts that can be numbers.
type MonthStyle
= NarrowMonth
| ShortMonth
| LongMonth
| NumericMonth
| TwoDigitMonth
| OmitMonth
Style options for the month.
type TimeZoneStyle
= ShortTimeZone
| LongTimeZone
| OmitTimeZone
Style options for the timeZoneName.
defaults
:
Intl.DateTimeFormat.Options
Returns the default options. This is helpful if you only care to change a
few options. If all the date time parts are left ommitted, then year, month,
and day will be assumed numeric.
options : Options
options =
{ defaults | weekday = ShortName }
resolvedOptions
:
Intl.DateTimeFormat.DateTimeFormat -> Intl.DateTimeFormat.Options
Returns the locale and formatting options computed when the DateTimeFormat was created.
if (resolvedOptions dateTimeFormat).hour12 then
"AM/PM"
else
"Military Time"
supportedLocalesOf
:
List Intl.Locale.Locale -> List Intl.Locale.Locale
Returns a list from the provided languages that are supported without having to fall back to the runtime's default language.
case fromLanguageTag "tlh" of
Just klingon ->
if isEmpty (supportedLocalesOf [ klingon ]) then
"I can't sort Klingon text"
else
"Make it so, Number One"
Nothing ->
"Khaaaaan!"