I have a datetime in milliseconds and I’d like to display it to a user in their browser locale.
I’m converting it into
Then I’m formatting it using
formatDateTime "YYYY-MM-DD HH:mm" from https://github.com/slamdata/purescript-formatters
The problem is that everything is formatted in UTC time.
Is there a similar library which would allow to use user’s locale for formatting?
I opted to use the ffi for this.
There is some discussion about time zones and the
purescript-datetime library in the following issue might be helpful to you. Knowing the right offset for a city requires querying a non-trivial data set, and performing calculations on a value having this offset as part of the value can be complicated. The PureScript library ecosystem does need an implementation of this, so we must wait for someone to tackle a proper implementation. Until then, you can use another language’s library using the FFI or write a quick-and-dirty implementation of a
LocalDateTime. Personally, I chose to use a MomentJS wrapper, which I should note might bring a huuuuuge timezone data set into your JS bundle, though it should be possible to trim it.
The solution that I have at the moment is to use
purescript-js-date (thanks @natefaubion) to get
https://pursuit.purescript.org/packages/purescript-js-date/6.0.0/docs/Data.JSDate#v:now combined with
After that I use
adjust before formatting:
formatMilliseconds :: Number -> Minutes -> String
formatMilliseconds timestamp timezoneOffset = fromMaybe "date-error" do
dt <- map toDateTime (instant $ Milliseconds timestamp)
adjusted <- adjust timezoneOffset dt
pure $ case FDT.formatDateTime "YYYY-MM-DD HH:mm" adjusted of
Left error -> error
Right formatted -> formatted
At the moment I don’t need more advanced localization, so timezone offset is all I need
Thanks for your help!