Skip to content

Instantly share code, notes, and snippets.

@felixyz
Forked from rjz/pretty-date.rb
Created September 17, 2012 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felixyz/3739894 to your computer and use it in GitHub Desktop.
Save felixyz/3739894 to your computer and use it in GitHub Desktop.
Pretty Date in Ruby
# 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