I had asked this question already:
How do I get the current time in Elm?
And answered it by writing my own (now deprecated) variant of start-ap
import Time exposing (Time)
import Html exposing (..)
import Html.Events exposing (onClick)
import Task
type Msg
= GetTime
| NewTime Time
type alias Model =
{ currentTime : Maybe Time
}
view : Model -> Html Msg
view model =
let
currentTime =
case model.currentTime of
Nothing ->
text ""
Just theTime ->
text <| toString theTime
in
div []
[ button [ onClick GetTime ] [ text "get time" ]
, currentTime
]
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
GetTime ->
model ! [ Task.perform NewTime Time.now ]
NewTime time ->
{ model | currentTime = Just time } ! []
main : Program Never Model Msg
main =
program
{ init = init
, update = update
, view = view
, subscriptions = always Sub.none
}
init : ( Model, Cmd Msg )
init =
{ currentTime = Nothing } ! []