Skip to content

Instantly share code, notes, and snippets.

@mahemoff
Last active September 13, 2018 13:25
Show Gist options
  • Save mahemoff/d24b5d7ec156bedffd594c8610178b7e to your computer and use it in GitHub Desktop.
Save mahemoff/d24b5d7ec156bedffd594c8610178b7e to your computer and use it in GitHub Desktop.
Abbreviated/short time ago
def short_time_ago(time, options={})
timestamp = time.iso8601
dist = DateTime.now.to_i - time.to_i
# Quick (non-i18n friendly hack to always show weeks instead of months (since "m" is ambiguous)
message =
if dist.to_i < 6.5.days || dist.to_i > 364.5.days
time_ago_in_words time, locale: :en_abbrev
else
"#{(dist/1.week).round}w"
end
message
# "<time class='short-timeago timeago' locale='en_abbrev' datetime='#{timestamp}'>#{message} ago</time>".html_safe # for use with jquery timeago
end
en:
datetime:
distance_in_words:
about_x_hours:
one: 1h
other: "%{count}h"
about_x_months:
one: 1M
other: "%{count}M"
about_x_years:
one: 1y
other: "%{count}y"
almost_x_years:
one: 1y
other: "%{count}y"
half_a_minute: 30s
less_than_x_minutes:
one: 30s
other: "-%{count}m"
less_than_x_seconds:
one: now
other: "-%{count}s"
over_x_years:
one: 1+ y
other: "%{count}+ y"
x_days:
one: 1d
other: "%{count}d"
x_minutes:
one: 1m
other: "%{count}m"
x_months:
one: 1M
other: "%{count}M"
x_seconds:
one: 1s
other: "%{count}s"
@simonrentzke
Copy link

Thanks for this! Just a comment on Rails 4.2, your file name doesn't match up with your locale, so the en_abbrev.yml file needs to start with en_abbrev:

@jfrux
Copy link

jfrux commented Sep 13, 2018

Works great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment