24 hour – 12 hour conversion in AS3

Posted on: September 23rd, 2010 by alemieux 3 Comments

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);


Tweet this! | Bookmark at Delicious

Tags: , ,

3 Responses

  1. Anonymous says:

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

  2. momochi says:

    Thanks for an idea, you sparked at thought from a angle I hadn’t given thoguht to yet. Now lets see if I can do something with it.

  3. Fat Loss says:

    thanks !! very helpful post!