# Date and Time conversion

**URL:** <https://discourse.elm-lang.org/t/date-and-time-conversion/9677>\
**Category:** Learn\
**Created:** [February 22, 2024, 11:19pm UTC](https://discourse.elm-lang.org/t/date-and-time-conversion/9677 "2024-02-22T23:19:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jefflembeck](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/jefflembeck/32/5151_2.png) [@jefflembeck](https://discourse.elm-lang.org/u/jefflembeck)\
**Post date:** [February 22, 2024, 11:19pm UTC](https://discourse.elm-lang.org/t/date-and-time-conversion/9677/1 "2024-02-22T23:19:05Z")

</div>

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](https://package.elm-lang.org/packages/rtfeldman/elm-iso8601-date-strings/latest/Iso8601), 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?

---

<div class="post-metadata">

**Author:** ![wolfadex](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/wolfadex/32/2461_2.png) [@wolfadex](https://discourse.elm-lang.org/u/wolfadex)\
**Post date:** [February 23, 2024, 3:46am UTC](https://discourse.elm-lang.org/t/date-and-time-conversion/9677/2 "2024-02-23T03:46:27Z")

</div>

I’d likely use [Time.Extra - time-extra 1.1.1](https://package.elm-lang.org/packages/justinmimbs/time-extra/latest/Time-Extra#partsToPosix) and get out the year/month/day like so

```elm
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)

---

<div class="post-metadata">

**Author:** ![lydell](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/lydell/32/178_2.png) [@lydell](https://discourse.elm-lang.org/u/lydell)\
**Post date:** [February 23, 2024, 10:03am UTC](https://discourse.elm-lang.org/t/date-and-time-conversion/9677/3 "2024-02-23T10:03:14Z")

</div>

Also, if you’re working with dates only (without time), I suggest you use the [date 4.0.1](https://elm.dmy.fr/packages/justinmimbs/date/latest/) package – it’s awesome!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [March 4, 2024, 10:03am UTC](https://discourse.elm-lang.org/t/date-and-time-conversion/9677/4 "2024-03-04T10:03:16Z")

</div>

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