AS3 Convert SECONDS TO HOURS, MINUTES, SECONDS

Here is a little Flash code snippet to convert the seconds from your audio or video time display project into hours, minutes and remaining seconds (HH:MM:SS). The code is for ActionScript 3.0 but to make it combatable with ActionScript 2.0. Just change the uint data type to Number and it will work fine.


function convertToHHMMSS($seconds:Number):String
{
	var s:Number = $seconds % 60;
	var m:Number = Math.floor(($seconds % 3600 ) / 60);
	var h:Number = Math.floor($seconds / (60 * 60));

	var hourStr:String = (h == 0) ? "" : doubleDigitFormat(h) + ":";
	var minuteStr:String = doubleDigitFormat(m) + ":";
	var secondsStr:String = doubleDigitFormat(s);

	return hourStr + minuteStr + secondsStr;
}

function doubleDigitFormat($num:uint):String
{
	if ($num < 10)
	{
		return ("0" + $num);
	}
	return String($num);
}

trace( convertToHHMMSS(10701) );

//Output = 02:58:21

2 Responses to “AS3 Convert SECONDS TO HOURS, MINUTES, SECONDS”

  1. Thanks for sharing that little snipet, was very handy!

  2. Thanks you sir!.
    This worked out of the box and spared me the thinking and development.
    Nice work.
    Much appreciated.
    -Ed

Leave a Reply