ADOBE FLEX 3
Developer Guide
123
Managing calendar dates and times
All of the calendar date and time management functions in ActionScript 3.0 are concentrated in the top-level Date
class. The Date class contains methods and properties that let you handle dates and times in either Coordinated
Universal Time (UTC) or in local time specific to a time zone. UTC is a standard time definition that is essentially
the same as Greenwich Mean Time (GMT).
Creating Date objects
The Date class boasts one of the most versatile constructor methods of all the core classes. You can invoke it four
different ways.
First, if given no parameters, the
Date() constructor returns a Date object containing the current date and time, in
local time based on your time zone. Heres an example:
var now:Date = new Date();
Second, if given a single numeric parameter, the Date() constructor treats that as the number of milliseconds since
January 1, 1970, and returns a corresponding Date object. Note that the millisecond value you pass in is treated as
milliseconds since January 1, 1970, in UTC. However, the Date object shows values in your local time zone, unless
you use the UTC-specific methods to retrieve and display them. If you create a new Date object using a single milli-
seconds parameter, make sure you account for the time zone difference between your local time and UTC. The
following statements create a Date object set to midnight on the day of January 1, 1970, in UTC:
var millisecondsPerDay:int = 1000 * 60 * 60 * 24;
// gets a Date one day after the start date of 1/1/1970
var startTime:Date = new Date(millisecondsPerDay);
Third, you can pass multiple numeric parameters to the Date() constructor. It treats those parameters as the year,
month, day, hour, minute, second, and millisecond, respectively, and returns a corresponding Date object. Those
input parameters are assumed to be in local time rather than UTC. The following statements get a Date object set to
midnight at the start of January 1, 2000, in local time:
var millenium:Date = new Date(2000, 0, 1, 0, 0, 0, 0);
Fourth, you can pass a single string parameter to the Date() constructor. It will try to parse that string into date or
time components and then return a corresponding Date object. If you use this approach, its a good idea to enclose
the
Date() constructor in a try..catch block to trap any parsing errors. The Date() constructor accepts a number
of different string formats, as listed in the ActionScript 3.0 Language and Components Reference. The following
statement initializes a new Date object using a string value:
var nextDay:Date = new Date(“Mon May 1 2006 11:30:00 AM”);
If the Date() constructor cannot successfully parse the string parameter, it will not raise an exception. However, the
resulting Date object will contain an invalid date value.
Getting time unit values
You can extract the values for various units of time within a Date object using properties or methods of the Date class.
Each of the following properties gives you the value of a time unit in the Date object:
The fullYear property
The month property, which is in a numeric format with 0 for January up to 11 for December
The date property, which is the calendar number of the day of the month, in the range of 1 to 31

Get ADOBE® FLEX® 3: PROGRAMMING ACTIONSCRIPT™ 3.0 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.