How do I get the current time in Elm?

后端 未结 8 1358
庸人自扰
庸人自扰 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 02:02

    You can use the Time package and/or the Date package.

    Here's a contrived example which uses both:

    import Signal
    import Time exposing (every, second)
    import Date exposing (year, hour, minute, second, fromTime)
    import Graphics.Element exposing (show)
    
    main =
      Signal.map currentTime (Time.every Time.second)
    
    currentTime t =
      let date' = fromTime t
          hour' = toString (Date.hour date')
          minute' = toString (Date.minute date')
          second' = toString (Date.second date')
          year' = toString (year date')
          now = "The current time is: " ++ hour' ++ ":" ++ minute' ++ ":" ++ second'
      in 
          show now
    

提交回复
热议问题