User Guide

198 Working with Dates and Times
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, 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 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