Created
October 17, 2015 11:14
-
-
Save cggaurav/72059051c3d34fbd2b5a to your computer and use it in GitHub Desktop.
Age and Format
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
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