Time_ago_in_words and distance_of_time_in_words for Phoenix/Elixir

https://gist.github.com/tlemens/88e9b08f62150ba6082f478a4a03ac52

MyApp.DistanceOfTimeHelpers.time_ago_in_words(~N[2015-03-05 21:45:00])
"about 6 years"
MyApp.DistanceOfTimeHelpers.distance_of_time_in_words(~N[2004-06-06 21:45:00], ~N[2004-06-06 21:45:00], include_seconds: true)
"less than 5 seconds"
MyApp.DistanceOfTimeHelpers.distance_of_time_in_words(~N[2004-06-06 21:45:00], ~N[2004-06-06 21:45:00])                       
"less than a minute"

Ported from Rails/Elixir

1 Like

Check out Cldr.DateTime.Relative for such functionality implemented in localisable fashion.

3 Likes

Also if you are using Timex -
https://hexdocs.pm/timex/Timex.Format.DateTime.Formatters.Relative.html
and
https://hexdocs.pm/timex/Timex.html#from_now/1

2 Likes

I forgot to mention that localization is supported: Copy the translations from here and port them to your gettext localization files

When calculating the time distance, leap years are also taken into account :wink: