Created
January 25, 2012 17:34
-
-
Save rvause/1677490 to your computer and use it in GitHub Desktop.
Fuzzy time since a given date
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
var fuzzy_timesince = function(when) { | |
parts = [ | |
['year', 60 * 60 * 24 * 365], | |
['month', 60 * 60 * 24 * 30], | |
['week', 60 * 60 * 24 * 7], | |
['day', 60 * 60 * 24], | |
['hour', 60 * 60], | |
['minute', 60] | |
]; | |
now = new Date(); | |
delta = (now - when) / 1000; | |
for (i in parts) { | |
val = parseInt(delta / parts[i][1]); | |
if (val >= 1) { | |
part = parts[i][0]; | |
break; | |
} | |
} | |
if (val <= 0) { | |
return 'just now'; | |
} | |
if (val > 1) { s = 's'} else { s = '' } | |
return val + ' ' + part + s; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment