Created
August 20, 2015 15:32
-
-
Save mattkelley/186bf943945964f00d10 to your computer and use it in GitHub Desktop.
format time nicely
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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