Created
January 24, 2012 19:05
-
-
Save rjz/1671922 to your computer and use it in GitHub Desktop.
Pretty Date in Ruby
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
# Nothin' special here: just Resig's pretty date function ported to Ruby | |
# http://ejohn.org/blog/javascript-pretty-date/ | |
def pretty_date(stamp) | |
now = Time.new | |
diff = now - stamp | |
day_diff = ((now - stamp) / 86400).floor | |
day_diff == 0 && ( | |
diff < 60 && "just now" || | |
diff < 120 && "1 minute ago" || | |
diff < 3600 && (diff / 60).floor.to_s + " minutes ago" || | |
diff < 7200 && "1 hour ago" || | |
diff < 86400 && (diff/3600).floor.to_s + " hours ago") || | |
day_diff == 1 && "Yesterday" || | |
day_diff < 7 && day_diff.to_s + " days ago" || | |
day_diff < 31 && (day_diff.to_s / 7).ceil + " weeks ago"; | |
end |
Yeah, Thanks for the observation. Felixyz.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The last line is wrong, should be:
day_diff < 31 && (day_diff/7).ceil.to_s + " weeks ago";