Anyone using input fields of type "datetime-local"? How to get Posix from that?

I’d love to use <input type="datetime-local" in my Elm app, but the output of that field seems to be of the form “YYYY-MM-DDTHH:MM”, and I’d like to send a plain Posix to the server. What’s a good way to convert that, given I already acquired a Zone?

Hi! You could manually split the string and use something like Time.Extra - time-extra 1.1.1 to construct the Posix.

it’s a bit of a hack but you can have a look at Iso8601 and maybe append the timezone to you string before parsing it or you deal with the timezone after you got UTC back


I hope you don’t mind a little advice:

Usually we try to always store/transfer etc. datetime-values based on UTC (Posix is fine too) - if you know that every value is based on +0 timezone you get into way less trouble with miss-matches/assumptions about which side/code (client, server, display, repository,…) has to handle local time.

Local-Timezones are only ever dealt with when displaying a time to the user (or receiving an time-input from a user)

1 Like

I don’t mind and even appreciate the advice! I’m also a bit torn on the issue. I’d love for the client to take the user’s time zone and send UTC to the server. That’s what I’m doing when I’m getting data from the server. However, with this datetime-local, I have a string that doesn’t have a time zone. And I cannot extend it properly with a time zone, at least not with the functions that elm/time provides - or can I?

I’m sure there is a package somewhere that let’s you get the hour-offset for a Zone but you can actually hack your way with what time provides by (ab)using toHour - the example there basically gives the hint :wink: - use millisToPosix to get a Posix for 0 and then subtract what you get from toHour to get the offset in hours.

That’s the solution I went for and it works beautifully, thanks!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.