Created
June 15, 2012 06:08
-
-
Save bcoughlan/2934948 to your computer and use it in GitHub Desktop.
Javascript - Format seconds to (hh:m)m:ss
This file contains 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
function formatTime (t) { | |
if (typeof (t) !== "number" || t < 0) { | |
return ""; | |
} | |
function it(param) { | |
var p = t % param; | |
if (p < 10) p = '0' + p; | |
t = Math.floor(t / param); | |
return p; | |
} | |
//Seconds | |
s=it(60); | |
if (t == 0) return "0:" + s; | |
//Minutes | |
m=it(60); | |
if (t == 0) return m + ":" + s; | |
//Hours | |
h=it(24); | |
if (t == 0) return h + ":" + m + ":" + s; | |
//Days | |
return t + ":" + h + ":" + m + ":" + s; | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment