You can use Json.Decode.indexto pull values ββby known indexes. You will need the values ββat indexes 0, 1, and 2, and then you can convert them to a string for use in the Date.fromStringfollowing way:
import Date exposing (Date)
import Html exposing (Html, text)
import Json.Decode exposing (..)
dateDecoder : Decoder Date
dateDecoder =
let
toDateString y m d =
String.join "-" (List.map toString [ y, m, d ])
in
map3 toDateString
(index 0 int)
(index 1 int)
(index 2 int)
|> andThen
(\str ->
case Date.fromString str of
Ok date ->
succeed date
Err err ->
fail err
)
You can use the decoder as follows:
decoder =
decode MyModel (field "date" dateDecoder)
source
share