Skip to content

Instantly share code, notes, and snippets.

@moresheth
Created April 27, 2012 17:03
Show Gist options
  • Save moresheth/2510841 to your computer and use it in GitHub Desktop.
Save moresheth/2510841 to your computer and use it in GitHub Desktop.
Caveman time formatting in Javascript
function formatTime( seconds ) {
seconds = Math.round( seconds );
var hours = 0,
minutes = 0;
while (seconds >= 3600) {
hours += 1;
seconds -= 3600;
}
while (seconds >= 60) {
minutes += 1;
seconds -= 60;
}
if ( hours > 0) {
return hours + ':' + (minutes < 10 ? '0' : '') + minutes + ':' + (seconds < 10 ? '0' : '') + seconds;
} else {
return minutes + ':' + (seconds < 10 ? '0' : '') + seconds;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment