Archive

Posts Tagged ‘Time’

AS Time Formatting

November 26th, 2007 alducente 1 comment

I’ve been holding on to this piece of code for almost a year now and I’ve used it several times for many different projects. I have been improving on it every time I used it and thought it was time to share it with everyone. It converts seconds into a nice time format, which can come in handy when working with video. In the example below, passing in 72 seconds in the parameter will return the same amount of time, but in a custom format (hh:mm:ss.ss).

trace(returnTime(72)); //traces "00:01:12.00"

function returnTime(seconds:Number):String{
	var minutes = Math.floor(seconds/60);
	var remainingSec = seconds % 60;
	var remainingMinutes = minutes % 60;
	var hours = Math.floor(minutes/60);
	var floatSeconds:Number = Math.floor((remainingSec - Math.floor(remainingSec))*100);
	remainingSec = Math.floor(remainingSec);

	//Edit the formatting below for a custom format
	return (getTwoDigits(hours)+":"+getTwoDigits(remainingMinutes)+":"+getTwoDigits(remainingSec)+"."+getTwoDigits(floatSeconds));
}

function getTwoDigits(number:Number):String{
	if(number < 10){
		return("0"+number);
	} else {
		return number+"";
	}
}
Categories: AS3 Tags: , ,