Another solution is using flags for this.
What we do is pass the time as a flag and later on get a Date
from that
In JS :
let flags = { initialTime: (new Date()).getTime() }
let app = Elm.App.init({ node, flags })
In Elm
type alias Flags =
{
initialTime : Posix
}
Decoding the flags looks like:
timeDecoder =
Decode.float
|> Decode.map round
|> Decode.map Time.millisToPosix
Decode.map Flags
Decode.field "initialTime" timeDecoder
And later when needed we convert the time to a Date
.
Date.fromPosix Time.utc model.flags.initialTime