Skip to content

Instantly share code, notes, and snippets.

@oliger
Created March 27, 2013 09:40
Show Gist options
  • Save oliger/5253018 to your computer and use it in GitHub Desktop.
Save oliger/5253018 to your computer and use it in GitHub Desktop.
function relativeTime(date) {
var units = {
year: 31557600000,
month: 2629800000,
week: 604800000,
day: 86400000,
hour: 3600000,
minute: 60000,
second: 1000
};
var delta = new Date() - new Date(date);
for (var unit in units) {
if (units.hasOwnProperty(unit)) {
if (delta >= units[unit]) {
var time = Math.floor(delta / units[unit]);
var unitName = unit + (time > 1 ? 's' : '');
return [time, unitName, 'ago'].join(' ');
}
}
}
return 'Now';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment