Thursday, September 23, 2010

24 hour - 12 hour conversion in AS3

Getting the current time is easy in AS3 with the Date() class, however the default clock is the 24 hour (military) clock.

Start by creating a new Date object:

var myDate:Date = new Date();

The Date class allows you to get to the month, day, hours, minutes, seconds and other Date-based information. If we try to trace out the time like this:

trace(myDate.hours + ":" + myDate.minutes + ":" + myDate.seconds);

We'll get 16 for the hours at 4 pm. The solution is really simple. Just subtract 12 from the hours to get the 12 hour hour.

trace(myDate.hours - 12 + ":" + myDate.minutes + ":" + myDate.seconds);




At 7:46 PM, Anonymous Anonymous said...

12AM registers as 0. Figured I'd add this comment because that's not exactly 12H format.


Post a Comment

<< Home