Flash Convert Military Time To Standard Time AS3

Here is a code snippet to show you how to get Standard Time and Military Time in ActionScript 3.

const STANDARD:uint = 12;
const MILITARY:uint = 24;

function getCurrentTime(timeFormat:uint):String 
{
	var date:Date = new Date();
	var hour:uint = date.getHours();
	var currentTime:String;
	var timeExtention:String;
		
	switch(timeFormat)
	{
		case 24:
			hour = (hour == 0) ? 12 : hour;
			timeExtention = "";
			break;
		case 12:
			if (hour > 12){
				hour = (hour == 12) ? 12 : hour - 12;
				timeExtention = " PM";
			}else{
				hour = (hour == 0) ? 12 : hour ;
				timeExtention = " AM";
			}
			break;
	}
	currentTime = hour.toString();
	currentTime += ":";
	currentTime += doubleDigitFormat( date.getMinutes() );
	currentTime += ":";
	currentTime += doubleDigitFormat( date.getSeconds() );
	currentTime += timeExtention;
		
	return currentTime;
}

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

trace( getCurrentTime(STANDARD) );
trace( getCurrentTime(MILITARY) );

Leave a Reply