Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails shorter "time_ago_in_words"

Is there a different time calculation in rails besides "time_ago_in_words"?? I want to be able to use just 'h' for hours 'd' days 'm' for months... ex. 3d, or 4h, or 5m

My code now...

<%= time_ago_in_words(feed_item.created_at) %> ago.
like image 689
pjmanning Avatar asked Sep 25 '12 23:09

pjmanning


1 Answers

In the newer versions of rails, you can specify a scope as an option for the method like so:

time_ago_in_words(company.updated_at, scope: 'datetime.distance_in_words.abbrv')

Then you just need to have your regular i18n file structured like so:

en:
  datetime:
    distance_in_words:
      abbrv:
        about_x_hours:
          one: ~ 1h
          other: ~ %{count}h
        about_x_months:
          one: ~ 1mo
          other: ~ %{count}mo
        about_x_years:
          one: ~ 1y
          other: ~ %{count}y
        almost_x_years:
...

This information is also available in the documentation: https://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#method-i-distance_of_time_in_words

like image 145
Fábio Batista Avatar answered Oct 31 '22 21:10

Fábio Batista