Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Time in Words - Ruby Helper
def time_ago_in_words(timestamp)
minutes = (((Time.now - timestamp).abs)/60).round
return nil if minutes < 0
case
when minutes < 1 then "less than a minute ago"
when minutes == 1 then "1 minute ago"
when minutes < 50 then "#{minutes} minutes ago"
when minutes < 90 then "1 hour ago"
when minutes < 1440 then "#{(minutes / 60).round} hours ago"
else
timestamp.strftime('%d %b %Y')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment