Skip to content

Instantly share code, notes, and snippets.

@millermedeiros
Created January 11, 2011 02:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save millermedeiros/773911 to your computer and use it in GitHub Desktop.
Save millermedeiros/773911 to your computer and use it in GitHub Desktop.
Format Time: seconds to time string
/**
* @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