Skip to content

Instantly share code, notes, and snippets.

@mamachanko
Created March 29, 2017 06:23
Show Gist options
  • Save mamachanko/8c61c9d27f5f2413740222803ad77a8e to your computer and use it in GitHub Desktop.
Save mamachanko/8c61c9d27f5f2413740222803ad77a8e to your computer and use it in GitHub Desktop.
Getting and formatting today in Elm
module Main exposing (..)
import Html exposing (Html, div, text, program)
import Mouse
import Keyboard
import Date
import Time
import Task
import Date.Format
-- MODEL
type alias Model =
Date.Date
init : ( Model, Cmd Msg )
init =
( Date.fromTime Time.second, Cmd.none )
-- MESSAGES
type Msg
= Success Date.Date
| Failure
| MouseClick Mouse.Position
-- VIEW
view : Model -> Html Msg
view model =
div []
[ text (Date.Format.format "%A, %d of %B %Y" model) ]
-- UPDATE
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case msg of
Success time ->
(time, Cmd.none)
Failure ->
model ! []
MouseClick _ ->
(model, Task.perform Success Date.now)
-- SUBSCRIPTIONS
-- subscriptions : Model -> Sub Msg
subscriptions model =
Sub.batch
[ Mouse.clicks MouseClick
]
-- MAIN
main : Program Never Model Msg
main =
program
{ init = init
, view = view
, update = update
, subscriptions = subscriptions
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment