Date and Time conversion

I’m new to Elm and am running into issues with converting date to time.

I have a date picker (I am using the native type=date input) and want to convert the value somebody chooses to Time.Posix

I have the current Time.Zone of the user and the value (which comes in YYYY-MM-DD format). I’m finding things like Iso8601 - elm-iso8601-date-strings 1.1.4, which nicely handles turning ISO 8601 strings into Time.Posix but I would like to figure a way to make this work with Zone so it does not assume that the user is picking dates with utc as the zone.

Does a library like this exist? If not, where might I start?

1 Like

I’d likely use Time.Extra - time-extra 1.1.1 and get out the year/month/day like so

fromDateString : Zone -> String -> Maybe Posix
fromDateString zone dateStr =
    case String.split "-" dateStr of
        [ yearStr, monthStr, dayStr ] ->
            Maybe.map3
                (\year month day ->
                    partsToPosix { ... }
                )
                (String.toInt yearStr)
                (String.toInt monthStr |> Maybe.andThen monthFromInt)
               (...)
         _ -> Nothing

monthFromInt : Int -> Time.Month

(On mobile so I didn’t want to type it all out)

1 Like

Also, if you’re working with dates only (without time), I suggest you use the date 4.0.1 package – it’s awesome!

2 Likes

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