A reliable way to format dates and times with Elm.
format : List Token -> Time.Zone -> Time.Posix -> String
This function takes in a list of tokens, Zone
, and Posix
to create your formatted string!
Let's say ourPosixValue
is November 15, 1993 at 15:06.
-- "15:06"
format
[ hourMilitaryFixed
, text ":"
, minuteFixed
]
utc
ourPosixValue
-- "3:06 pm"
format
[ hourNumber
, text ":"
, minuteFixed
, text " "
, amPmLowercase
]
utc
ourPosixValue
-- "Nov 15th, 1993"
format
[ monthNameFirstThree
, text " "
, dayOfMonthSuffix
, text ", "
, yearNumber
]
utc
ourPosixValue
formatWithLanguage : Language -> List Token -> Time.Zone -> Time.Posix -> String
If our users don't speak English, printing out "Monday" or "Tuesday" might not be a great fit.
Thanks to a great recommendation, date-format
now supports multilingual output!
All you need to do is provide your own options, and format will use your preferences instead:
For a complete example, check out the FormatWithOptions.elm
in the examples folder.
These are the available tokens to help you format dates.
monthNumber : Token
Get the numeric value of the month.
Examples: 1, 2, 3, ... 11, 12
monthSuffix : Token
Get the numeric value of the month, with a suffix at the end.
Examples: 1st, 2nd, 3rd, ... 11th, 12th
monthFixed : Token
Get the numeric value of the month, fixed to two places.
Examples: 01, 02, 03, ... 11, 12
monthNameAbbreviated : Token
Get the name of the month, but abbreviated (abbreviation function comes from the language settings)
Examples: Jan, Feb, Mar, ... Nov, Dec
monthNameFull : Token
Get the full name of the month.
Examples: January, February, ... December
dayOfMonthNumber : Token
Get the numeric value of the day of the month.
Examples: 1, 2, 3, ... 30, 31
dayOfMonthSuffix : Token
Get the numeric value of the day of the month, with a suffix at the end.
Examples: 1st, 2nd, 3rd, ... 30th, 31st
dayOfMonthFixed : Token
Get the numeric value of the day of the month, fixed to two places.
Examples: 01, 02, 03, ... 30, 31
dayOfYearNumber : Token
Get the numeric value of the day of the year.
Examples: 1, 2, 3, ... 364, 365
dayOfYearSuffix : Token
Get the numeric value of the day of the year, with a suffix at the end.
Examples: 1st, 2nd, 3rd, ... 364th, 365th
dayOfYearFixed : Token
Get the numeric value of the day of the year, fixed to three places.
Examples: 001, 002, 003, ... 364, 365
dayOfWeekNumber : Token
Get the numeric value of the day of the week.
Examples: 0, 1, 2, ... 5, 6
dayOfWeekSuffix : Token
Get the numeric value of the day of the week, with a suffix at the end.
Examples: 0th, 1st, 2nd, ... 5th, 6th
dayOfWeekNameAbbreviated : Token
Gets the name of the day of the week, but just the first two letters.
Examples: Su, Mo, Tu, ... Fr, Sa
dayOfWeekNameFull : Token
Gets the full name of the day of the week.
Examples: Sunday, Monday, ... Friday, Saturday
yearNumberLastTwo : Token
Get the year, but just the last two letters.
Examples: 70, 71, ... 29, 30
yearNumber : Token
Get the year.
Examples: 1970, 1971, ... 2018, ... 9999, ...
quarterNumber : Token
Get the numeric value for the quarter of the year.
Examples: 1, 2, 3, 4
quarterSuffix : Token
Get the numeric value for the quarter of the year, with a suffix.
Examples: 1st, 2nd, 3rd, 4th
weekOfYearNumber : Token
Get the numeric value for the week of the year.
Examples: 1, 2, 3, ... 51, 52
weekOfYearSuffix : Token
Get the numeric value for the week of the year, with a suffix at the end.
Examples: 1st, 2nd, 3rd, ... 51st, 52nd
weekOfYearFixed : Token
Get the numeric value for the week of the year, fixed to two places.
Examples: 01, 02, 03, ... 51, 52
amPmUppercase : Token
Get the AM / PM value of the hour, in uppercase.
Examples: AM, PM
amPmLowercase : Token
Get the AM / PM value of the hour, in uppercase.
Examples: am, pm
hourMilitaryNumber : Token
Get the hour of the 24-hour day.
Examples: 0, 1, 2, ... 22, 23
hourMilitaryFixed : Token
Get the hour of the 24-hour day, fixed to two places.
Examples: 00, 01, 02, ... 22, 23
hourNumber : Token
Get the hour of the 12-hour day.
Examples: 0, 1, 2, ... 11, 12
hourFixed : Token
Get the hour of the 12-hour day, fixed to two places.
Examples: 00, 01, 02, ... 11, 12
hourMilitaryFromOneNumber : Token
Get the hour of the 24-hour day, starting from one.
Examples: 1, 2, ... 23, 24
hourMilitaryFromOneFixed : Token
Get the hour of the 24-hour day, starting from one, fixed to two places.
Examples: 01, 02, ... 23, 24
minuteNumber : Token
Get the minute of the hour.
Examples: 0, 1, 2, ... 58, 59
minuteFixed : Token
Get the minute of the hour, fixed to two places.
Examples: 00, 01, 02, ... 58, 59
secondNumber : Token
Get the second of the minute.
Examples: 0, 1, 2, ... 58, 59
secondFixed : Token
Get the second of the minute, fixed to two places.
Examples: 00, 01, 02, ... 58, 59
millisecondNumber : Token
Get the milliseconds of the second.
Examples: 0, 1, 2, ... 998, 999
millisecondFixed : Token
Get the milliseconds of the second, fixed to three places.
Examples: 000, 001, 002, ... 998, 999
text : String -> Token
Represent a string value
formatter : Zone -> Posix -> String
formatter =
DateFormat.format
[ DateFormat.hourMilitaryFixed
, DateFormat.text ":"
, DateFormat.minuteFixed
]
When given a Zone
and Posix
, this will return something like "23:15"
or "04:43"