I wrote a little function which formats a ms timestamp in a human readable way. I know there are lots of scripts out there, but I needed a very simple one, which only outputs minutes and hours.
I was wondering if there is any way to shorten or improve this code snippet, because to me it seems to be very long for the few things it actually does.
function prettyTime(ms) {
var sec = ms / 1000,
time,
timeUnit;
if(sec > 3600) {
// Hours
time = Math.round(sec / 3600);
timeUnit = ' hr';
} else if(sec > 60) {
// Minutes
time = Math.round(sec / 60);
timeUnit = ' min';
} else {
return 'less than 1 min';
}
if(time > 1) {
timeUnit += 's';
}
return 'about ' + time + timeUnit + 'ago';
}
alert(prettyTime(7600001));