How to get the current date in elms?

I am trying to figure out how to get the current date in elms in version .17. I see that they added the Date module in .17, but I have not found examples of how it is used. Has anyone figured out how to do this?

Edit: While trying to modify this solution, I hit another stumbling block. I am trying to set a date and then call another Msg to do something else. But I'm still getting {} for the date.

import Html.App as App
import Html exposing (..)
import Time exposing (Time)
import Task
import Date exposing (Date)
import Html.Events exposing (onClick)
import Html.Attributes exposing (..)

type alias Model =
  {currentDate : Maybe Date}

type Msg =
  SetDate (Maybe Date)
  | TriggerDateSet

update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    SetDate date ->
      ({model | currentDate = date}, Cmd.none)
    TriggerDateSet ->
      (model, now)

view : Model -> Html Msg
view model =
  div []
  [ div []
    [ button [onClick TriggerDateSet] [] ]
  , div [] [ text <| "(Optional) time at program launch was " ++ toString model ]
  ]

now : Cmd Msg
now =
  Task.perform (always (SetDate Nothing)) (Just >> SetDate) Date.now

main : Program Never
main =
  App.program
    { init = ( Model Nothing, now )
    , view = view
    , subscriptions = always Sub.none
    , update = update
    }
0
source share
2 answers

You will need a nowTask or a everySubscription from Time .

Here is an example of using the first to initialize the model with the current time.

import Html.App as App
import Html exposing (..)
import Time exposing (Time)
import Task


type alias Model = 
  Maybe Time


type Msg = 
  SetTime (Maybe Time)


update : Msg -> Model -> (Model, Cmd Msg)
update (SetTime time) _ = 
  (time, Cmd.none)


view : Model -> Html Msg
view model =
  div [] [ text <| "(Optional) time at program launch was " ++ toString model ]


now : Cmd Msg
now = 
  Task.perform (Just >> SetTime) Time.now


main : Program Never
main =
  App.program 
    { init = ( Nothing, now ) 
    , view = view
    , subscriptions = always Sub.none 
    , update = update
    }
+4
source

+1 @SorenDebois. .

myDate = Date.fromTime timeFromMaybe

Date . ( ).

import Html.App as App
import Html exposing (..)
import Date exposing (Date)
import Task


type alias Model = 
  Maybe Date


type Msg = 
  SetDate (Maybe Date)


update : Msg -> Model -> (Model, Cmd Msg)
update (SetDate date) _ = 
  (date, Cmd.none)


view : Model -> Html Msg
view model =
  div [] [ text <| "(Optional) date at program launch was " ++ dateString model ]


dateString : Model -> String
dateString model =
  case model of
    Nothing -> "No date here"
    Just date ->
      "the date is "
      ++ (toString <| Date.dayOfWeek date)
      ++ " "
      ++ (toString <| Date.day date)
      ++ " "
      ++ (toString <| Date.month date)
      ++ " "
      ++ (toString <| Date.year date)

now : Cmd Msg
now = 
  Task.perform (Just >> SetDate) Date.now


main : Program Never
main =
  App.program 
    { init = ( Nothing, now ) 
    , view = view
    , subscriptions = always Sub.none 
    , update = update
    }

.

+1

Source: https://habr.com/ru/post/1667494/


All Articles