Warius
Warius

Reputation: 75

Date Display with elm

How can I display a date with elm? I used toString but display is too long.

let
    date =
        Date.fromTime <| toFloat risk.last_modified
in
    tr []
       [ td [] [ text risk.id ]
       , td [] [ text <| toString date ]
       , td [] [ text risk.title ]
       , td [] [ text risk.admin.comment ]
       , td [] <| badge risk.admin.status
       ]

Upvotes: 2

Views: 635

Answers (1)

Natim
Natim

Reputation: 18102

Using elm-strftime

You can use elm-strftime:

elm-package install thaterikperson/elm-strftime

And then:

import Strftime exposing (format)

showDate : Date -> String
showDate date =
    format "%A %B %d %Y %H:%M" date

Old version using elm-time

Previously I used elm-time like that:

showDate : DateTime -> String
showDate datetime =
    let
        day =
            DateTime.day datetime

        month =
            DateTime.month datetime

        year =
            DateTime.year datetime

        hour =
            DateTime.hour datetime

        minute =
            DateTime.minute datetime
    in
        (zfill day) ++ "/" ++ (zfill month) ++ "/" ++ (toString year) ++ " " ++ (zfill hour) ++ ":" ++ (zfill minute)


zfill : Int -> String
zfill value =
    let
        string =
            toString value
    in
        if (String.length string) == 1 then
            "0" ++ string
        else
            string

You can also use a ZonedDateTime if you want to display the time in the correct TimeZone.

Upvotes: 3

Related Questions