Created
January 11, 2011 02:38
-
-
Save millermedeiros/773911 to your computer and use it in GitHub Desktop.
Format Time: seconds to time 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
/** | |
* @param {number} seconds Number between 0 to 359999. | |
* @return {string} Time string on the format 'HH:MM:SS'. | |
* @author Miller Medeiros | |
*/ | |
function formatTime(seconds){ | |
var output = Math.floor(seconds/3600) +':'+ ((Math.floor(seconds/60)%60) ^ 0) +':'+ ((seconds%60) ^ 0); //XOR 0 removes decimal digits | |
return output.replace(/([^0-9]|^)([0-9])(?=[^0-9]|$)/g, '$10$2'); //add zero before single digits | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment