Skip to content

Instantly share code, notes, and snippets.

@aackerman
Created April 13, 2012 01:54
Show Gist options
  • Save aackerman/2372854 to your computer and use it in GitHub Desktop.
Save aackerman/2372854 to your computer and use it in GitHub Desktop.
Fuzzy Time
Convert Unix time in seconds to a fuzzy timestamp since the current time: 4s, 9m, 2d, 1m, etc.
var fuzzy_time = function (a) {
var b = new Date,
c = parseInt(((b.getTime() / 1e3) - a), 10),
d = "";
return c < 60 ? d = c + "s" : c < 120 ? d = "1m" : c < 2700 ? d = parseInt(c / 60, 10).toString() + "m" : c < 7200 ? d = "1h" : c < 86400 ? d = "" + parseInt(c / 3600, 10).toString() + "h" : c < 172800 ? d = "1d" : d = parseInt(c / 86400, 10).toString() + "d", d
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment