ryannhg/date-format/2.2.0

NOTE: the latest version is 2.3.0

Renamed to ryan-haskell/date-format. It is published under that name, and new versions come out there.

Versions published as ryannhg/date-format still install under that name. 6 versions of 6 come from the new location (see their notes).

Rerouted (renamed, verified). The GitHub user or repository was renamed. The tag is still there, at the new name. The Lamdera package server serves a copy recovered from https://github.com/ryan-haskell/date-format.

What we checked: The repo moved to github.com/ryan-haskell/date-format. Rebuilt with git archive from commit bc8cb4957161 of https://github.com/ryan-haskell/date-format (zip prefix ryannhg-date-format-5b59e59/, America/Los_Angeles times): the zip is byte-identical to the one package.elm-lang.org recorded, SHA-1 8557d20203b192dd20c654b20efe9f931ec0a0e1.

The Lamdera compiler uses this copy and prints a one-line notice saying so. elm itself fails to download this version.

Details · package.elm-lang.org's zip: https://github.com/ryannhg/date-format/zipball/2.2.0/

DateFormat.Relative

A reliable way to get a pretty message for the relative time difference between two dates.

Getting relative time for two dates

relativeTime : Posix -> Posix -> String

This function takes in two times and returns the relative difference!

Here are a few examples to help:

relativeTime now tenSecondsAgo == "just now"


relativeTime now tenSecondsFromNow == "in a few seconds"


relativeTime now fortyThreeMinutesAgo == "43 minutes ago"


relativeTime now oneHundredDaysAgo == "100 days ago"


relativeTime now oneHundredDaysFromNow == "in 100 days"




-- Order matters!
relativeTime now tenSecondsAgo == "just now"


relativeTime tenSecondsAgo now == "in a few seconds"
relativeTimeWithOptions : RelativeTimeOptions -> Posix -> Posix -> String

Maybe relativeTime is too lame. (Or maybe you speak a different language than English!)

With relativeTimeWithOptions, you can provide your own custom messages for each time range.

(That's what relativeTime uses under the hood!)

You can provide a set of your own custom options, and use relativeTimeWithOptions instead.

type alias RelativeTimeOptions =
{ someSecondsAgo : Int -> String
, someMinutesAgo : Int -> String
, someHoursAgo : Int -> String
, someDaysAgo : Int -> String
, someMonthsAgo : Int -> String
, someYearsAgo : Int -> String
, rightNow : String
, inSomeSeconds : Int -> String
, inSomeMinutes : Int -> String
, inSomeHours : Int -> String
, inSomeDays : Int -> String
, inSomeMonths : Int -> String
, inSomeYears : Int -> String
}

Options for configuring your own relative message formats!

For example, here is how someSecondsAgo is implemented by default:

defaultSomeSecondsAgo : Int -> String
defaultSomeSecondsAgo seconds =
    if seconds < 30 then
        "just now"


    else
        toString seconds ++ " seconds ago"

And here is how inSomeHours might look:

defaultInSomeHours : Int -> String
defaultInSomeHours hours =
    if hours < 2 then
        "in an hour"


    else
        "in " ++ toString hours ++ " hours"
defaultRelativeOptions : RelativeTimeOptions

If there is something you'd like to tweak based off of the defaults, this record might be a good starting point!