Skip to content

Instantly share code, notes, and snippets.

@cggaurav
Created October 17, 2015 11:14
Show Gist options
  • Save cggaurav/72059051c3d34fbd2b5a to your computer and use it in GitHub Desktop.
Save cggaurav/72059051c3d34fbd2b5a to your computer and use it in GitHub Desktop.
Age and Format
function calcAge(isodatestring) {
if (isodatestring)
return Math.round(new Date() - Date.parse(isodatestring)) / (24*60*60*1000);
return 0;
}
function formatAge(age) {
var ret2 = '';
var mins_ago = age * 24 * 60;
if (mins_ago <= 1) {
ret2 += 'Just now';
} else if (mins_ago <= 60) {
ret2 += Math.round(mins_ago)+'m';
} else {
var hours_ago = age * 24;
if (hours_ago <= 24) {
ret2 += Math.round(hours_ago)+'h';
}
else {
var days_ago = age;
ret2 += Math.round(days_ago)+'d';
}
}
return ret2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment