Skip to content

Instantly share code, notes, and snippets.

@rjz
Created January 24, 2012 19:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rjz/1671922 to your computer and use it in GitHub Desktop.
Save rjz/1671922 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 < 31 && (day_diff.to_s / 7).ceil + " weeks ago";
end
@felixyz
Copy link

felixyz commented Sep 17, 2012

The last line is wrong, should be:

day_diff < 31 && (day_diff/7).ceil.to_s + " weeks ago";

@phundi
Copy link

phundi commented Jan 11, 2019

Yeah, Thanks for the observation. Felixyz.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment