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. Here’s 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 milliseconds
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,
it’s 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 (which are listed in the
ActionScript 3.0 Reference for the Adobe
Flash Platform
). 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.