Skip to content

Instantly share code, notes, and snippets.

@bcoughlan
Created June 15, 2012 06:08
Show Gist options
  • Save bcoughlan/2934948 to your computer and use it in GitHub Desktop.
Save bcoughlan/2934948 to your computer and use it in GitHub Desktop.
Javascript - Format seconds to (hh:m)m:ss
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