Print date/time as text like '3 days ago'

81 views
Skip to first unread message

Jacky See

unread,
Sep 30, 2016, 12:28:42 AM9/30/16
to Elm Discuss
I'm wondering how can I print a date/time as '3 days ago' like this library http://timeago.yarp.com/ ?

What I can think of is to use subscription and Time.every to pass current date time to model for rendering.
Is it possible to update current date to model only when update function is called?

Max Goldstein

unread,
Sep 30, 2016, 2:24:38 AM9/30/16
to Elm Discuss
I think there are two parts here.

A "time ago in words" library as a pure function of two dates (now and the date in question). This is independent of where the dates come from.

A subscription that provides you the "now" date at a frequency you specify, which gets stored in the model. I don't think there's a way to ask for the date only when update would otherwise be called, and you don't want that (the time should update if the user leaves the computer alone for ten minutes).

Jacky

unread,
Sep 30, 2016, 2:33:06 AM9/30/16
to elm-d...@googlegroups.com
I can think of two path to update the current time:
- through subscription to Time.every, set duration like 1 min
- through update command like:

update msg mode =
    case msg of
        OtherAction ->
            ...
            { model | ... } ! [ Task.perform (\_ -> NoOp) UpdateTime Time.now ]

        UpdateTime time ->
            { model | currentTime time } ! []

        NoOp ->
            model ! []



--
You received this message because you are subscribed to a topic in the Google Groups "Elm Discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elm-discuss/G32oBI6yAxw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elm-discuss+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Best Regards,
Jacky See
Reply all
Reply to author
Forward
0 new messages