Skip to content

Instantly share code, notes, and snippets.

@timsavery
Created May 16, 2012 16:39
Show Gist options
  • Save timsavery/2712015 to your computer and use it in GitHub Desktop.
Save timsavery/2712015 to your computer and use it in GitHub Desktop.
function getTimeSince(start, end) {
var msSince = (end - start);
var msInDay = 24*60*60*1000
, msInHour = 60*60*1000
, msInMinute = 60*1000
, msInSecond = 1000;
if (msSince > msInDay) { // greater than one day
return parseInt(msSince / msInDay).toString() + 'd Ago';
} else if (msSince > msInHour) { // greater than one hour
return parseInt(msSince / msInHour).toString() + 'h Ago';
} else if (msSince > msInMinute) { // greater than one minute
return parseInt(msSince / msInMinute).toString() + 'm Ago';
} else { // seconds ago
var sSince = parseInt(msSince / msInSecond);
if (sSince > 0) {
return sSince.toString() + 's Ago';
} else {
return "Just Now";
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment