AS Time Formatting

Reading Time: 1 minute

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 (

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){
	} else {
		return number+"";

One Reply to “AS Time Formatting”

  1. It will compile to the exact same bytecode, I’m sure, but for code compactness (which I favor in my utility functions), there’s always:

    function getTwoDigits(number:Number):String {
    return (number > 10)? number : “0”+number;

    I switched the statement blocks and comparison sign around, for no reason other than personal preference. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *