Skip to content

Instantly share code, notes, and snippets.

@rvause
Created January 25, 2012 17:34
Show Gist options
  • Save rvause/1677490 to your computer and use it in GitHub Desktop.
Save rvause/1677490 to your computer and use it in GitHub Desktop.
Fuzzy time since a given date
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