-
-
Save felixyz/3739894 to your computer and use it in GitHub Desktop.
Pretty Date in Ruby
This file contains hidden or 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 < 14 && "one week ago" || | |
day_diff < 31 && (day_diff/7).ceil.to_s + " weeks ago"; | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment