date-format
A reliable way to format dates and times with Elm.
elm package
Using theelm install ryannhg/date-format
date-format
?
What is If you’re coming from Javascript, you might have heard of MomentJS.
MomentJS is a great library for formatting dates!
date-format
has similar formatting options as Moment, but it uses Elm’s awesome type system to provide human readable names, and catch typos for you at compile time!
No need to remember the difference between mm
and MM
and M
!
A quick example
import DateFormat
import Time exposing (Posix, Zone, utc)
-- Let's create a custom formatter we can use later:
ourFormatter : Zone -> Posix -> String
ourFormatter =
DateFormat.format
[ DateFormat.monthNameFull
, DateFormat.text " "
, DateFormat.dayOfMonthSuffix
, DateFormat.text ", "
, DateFormat.yearNumber
]
-- With our formatter, we can format any date as a string!
ourTimezone : Zone
ourTimezone =
utc
-- 2018-05-20T19:18:24.911Z
ourPosixTime : Posix
ourPosixTime =
Time.millisToPosix 1526843861289
ourPrettyDate : String
ourPrettyDate =
ourFormatter ourTimezone ourPosixTime
Would make ourPrettyDate
return:
"May 20th, 2018" : String
Want more examples?
I’ve created a few more examples in the examples/
folder for this repo.
Here’s how you can try them out:
-
git clone https://github.com/ryannhg/date-format
-
cd date-format/examples
-
elm reactor
-
Go to http://localhost:8000