All Downloads are FREE. Search and download functionalities are using the official Maven repository.

elm.DateOnly.mustache Maven / Gradle / Ivy

There is a newer version: 7.8.0
Show newest version
module DateOnly exposing (DateOnly, dateOnlyDecoder, dateOnlyEncoder)

import Date
import Date.Extra exposing (fromIsoString, toFormattedString)
import Json.Decode as Decode exposing (Decoder)
import Json.Encode as Encode


type alias DateOnly =
    Date.Date


dateOnlyDecoder : Decoder DateOnly
dateOnlyDecoder =
    Decode.string
        |> Decode.andThen decodeIsoString


dateOnlyEncoder : DateOnly -> Encode.Value
dateOnlyEncoder model =
    Encode.string <| toFormattedString "yyyy-MM-dd" model


decodeIsoString : String -> Decoder DateOnly
decodeIsoString str =
    case fromIsoString str of
        Just date ->
            Decode.succeed date

        Nothing ->
            Decode.fail <|
                "Cannot convert "
                    ++ str
                    ++ " to DateOnly"




© 2015 - 2024 Weber Informatics LLC | Privacy Policy