Created
November 18, 2011 16:07
-
-
Save edwinwebb/1376880 to your computer and use it in GitHub Desktop.
Javascript Media Seconds to MM:SS:MS string
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
secondsToTimeString = function (seconds) { | |
var ms = Math.floor((seconds*1000) % 1000); | |
var s = Math.floor(seconds%60); | |
var m = Math.floor((seconds*1000/(1000*60))%60); | |
var strFormat = "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
I believe there is a small bug in this code. Line 10 should be
if(ms < 100) ms = "0" + ms;
.Without the fix, the function would run as follows:
But this is incorrect, as it should output "00:00:05".