Skip to content

Instantly share code, notes, and snippets.

@mattkelley
Created August 20, 2015 15:32
Show Gist options
  • Save mattkelley/186bf943945964f00d10 to your computer and use it in GitHub Desktop.
Save mattkelley/186bf943945964f00d10 to your computer and use it in GitHub Desktop.
format time nicely
/**
* Utility for formating seconds into pretty time formats
* @OUTPUT as follows: 0:01, 1:01, 10:01, 1:10:01
**/
function formatTime(seconds) {
// video.duration returns NaN before loadedmetadata fires
var seconds = isNaN(seconds) ? 0 : seconds.toFixed();
var h = parseInt(seconds / 3600) % 24;
var m = parseInt(seconds / 60) % 60;
var s = seconds % 60;
if (h < 1) {
// under an hour
return m + ':' + (s < 10 ? '0' + s : s);
}
// over an hour
return h + ':' + (m < 10 ? '0' + m : m) + ':' + (s < 10 ? '0' + s : s);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment