Skip to content

Instantly share code, notes, and snippets.

@mzabriskie
Created September 11, 2013 05:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mzabriskie/6519685 to your computer and use it in GitHub Desktop.
Save mzabriskie/6519685 to your computer and use it in GitHub Desktop.
function duration(timeInMillis) {
var intervals = {
'h': 3600000,
'm': 60000,
's': 1000,
'ms': 1
},
sb = '';
for (var k in intervals) {
if (!intervals.hasOwnProperty(k)) continue;
var v = intervals[k],
unit = Math.floor(timeInMillis / v);
timeInMillis %= v;
if (unit > 0 || (sb.length === 0 && k === 'ms')) {
if (sb.length > 0) sb += ':';
sb += unit + k;
}
}
return sb;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment