Skip to content

Instantly share code, notes, and snippets.

@edwinwebb
Created March 13, 2013 11:21
Show Gist options
  • Select an option

  • Save edwinwebb/5151220 to your computer and use it in GitHub Desktop.

Select an option

Save edwinwebb/5151220 to your computer and use it in GitHub Desktop.
Format MS to Time Stamp "MM:SS:XX" in Javascript
function formatSeconds(seconds, format) {
var ms = Math.floor((seconds*1000) % 1000);
var s = Math.floor(seconds%60);
var m = Math.floor((seconds*1000/(1000*60))%60);
var strFormat = format || "MM:SS:XX";
if(s < 10) s = "0" + s;
if(m < 10) m = "0" + m;
if(ms < 10) ms = "0" + ms;
strFormat = strFormat.replace(/MM/, m);
strFormat = strFormat.replace(/SS/, s);
strFormat = strFormat.replace(/XX/, ms.toString().slice(0,2));
return strFormat;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment