Skip to content

Instantly share code, notes, and snippets.

@mattupstate
Created May 26, 2010 17:25
Show Gist options
  • Save mattupstate/414769 to your computer and use it in GitHub Desktop.
Save mattupstate/414769 to your computer and use it in GitHub Desktop.
/**
* Converts the amount of milliseconds into a string based time code.
* @param milliseconds
* @param delimiter
* @param withHours
* @return The time code as a string.
*/
function getTimeCode( milliseconds:Number, delimeter:String = ":", withHours:Boolean = false ):String
{
var posHours:Number = Math.floor( milliseconds / 1000 / 60 / 60 );
var posMins:Number = Math.floor( milliseconds / 1000 / 60 );
var posSecs:Number = Math.round( milliseconds / 1000 % 60 );
if( posSecs >= 60 )
{
posSecs = 0;
posMins++;
}
if( posMins >= 60 )
{
posMins = 0;
posHours++;
}
var timeHours:String = ( posHours < 10 ) ? "0" + posHours.toString() : posHours.toString();
var timeMins:String = ( posMins < 10 ) ? "0" + posMins.toString() : posMins.toString();
var timeSecs:String = ( posSecs < 10 ) ? "0" + posSecs.toString() : posSecs.toString();
var result:String = timeMins + delimeter + timeSecs;
if( withHours )
{
result = timeHours + delimeter + result;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment