Home The Computus Engine Inside the AS3 Date class: The constructor

Inside the AS3 Date class: The constructor

written by John April 10, 02008

The AS3 Date class constructor is very accommodating: It actually supports four separate ways of defining a new Date object. Each of the following examples creates an instance of a Date object called ‘d’.

The simplest syntax is this:

If you pass no parameters into the constructor the object is populated with the current time and date.

If you pass a single number into the constructor then you are setting the base value of the Date directly. As described earlier this value is the total number of milliseconds before or since the epoch. As this value is stored in UTC you should also supply it in UTC. The above example sets the date and time to be 5 seconds past midnight on 1st January 1970.

If you pass a series of numbers into the constructor then you are setting the individual date and time values of the Date. The order is as follows: year, month, dayOfMonth, hour, minute, second, millisecond. The first example sets the date and time (in local time) to be 1 second past midnight on the 1st January 2000. The second example sets the same values in UTC time.

This last type of construction is new in AS3. If you pass a valid string into the constructor then the Date class will parse this into a date and time. The example above sets the date and time, in local time, to be 15.30am on the 12th April 2008. The following formats, and partial versions (month, day, year only) of these versions are also supported:

Year, month and day terms can be separated by a forward slash or a space, but never a dash. TZD (Time Zone Designation) should be in the form UTC-HHMM where HHMM indicates the hours and minutes offset relative to UTC. You can test the validity of a string by using the parse() method.

You may also like

Leave a Reply