Skip to content

Instantly share code, notes, and snippets.

@s4y
Created April 16, 2012 03:35
Show Gist options
  • Save s4y/2396233 to your computer and use it in GitHub Desktop.
Save s4y/2396233 to your computer and use it in GitHub Desktop.
JavaScript function for formatting a duration of time
function formatDuration(duration){
var seconds = Math.abs(Math.ceil(duration / 1000)),
h = (seconds - seconds % 3600) / 3600,
m = (seconds - seconds % 60) / 60 % 60,
s = seconds % 60;
return (duration < 0 ? '-' : '') + h + ':' + zeroPad(m.toString(), 2) + ':' + zeroPad(s.toString(), 2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment