Last active
September 19, 2024 09:03
-
-
Save umutakturk/3808180 to your computer and use it in GitHub Desktop.
Ruby readable time ago function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def time_ago(timestamp) | |
delta = Time.now.to_i - timestamp | |
case delta | |
when 0..30 then "just now" | |
when 31..119 then "about a minute ago" | |
when 120..3599 then "#{delta / 60} minutes ago" | |
when 3600..86399 then "#{(delta / 3600).round} hours ago" | |
when 86400..259199 then "#{(delta / 86400).round} days ago" | |
else Time.at(timestamp).strftime('%d %B %Y %H:%M') | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment