How do I get the current time in Elm?

后端 未结 8 1339
庸人自扰
庸人自扰 2020-12-03 01:15

I\'m running elm-repl to play around with the language.

I\'d like to see what the current time is. How would I do that? It doesn\'t appear to be possible with the c

8条回答
  •  一生所求
    2020-12-03 01:58

    Update for 0.19 It is not possible to get the current time using the standard library.. You need to use elm/time. As with 0.18, all you need is a command and Msg to handle the result

    type Msg
        = OnTime Time.Posix 
    
    getTime : Cmd Msg
    getTime = 
        Task.perform OnTime Time.now 
    

    Update for 0.18 This has got simpler again. Now all you need is a command and Msg to handle the result

    type Msg
        = OnTime Time 
    
    getTime : Cmd Msg
    getTime = 
        Task.perform OnTime Time.now 
    

    See this Ellie

    Original answer

    With 0.17, this got a whole lot easier. There is now a Task in the Time library. So for example, we now have:

    Time.now
    |> Task.Perform NoOp CurrentTime
    

提交回复
热议问题