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

Inside the AS3 Date class: The constructor

by John

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:

var d:Date = new Date()

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

var d:Date = new Date( 5000 )

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.

var d:Date = new Date( 2000, 0, 1, 0, 0, 1, 0 )
var d:Date = new Date( Date.UTC(2000, 0, 1, 0, 0, 1, 0) ) 

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.

var d:Date = new Date( 'Sun Jun 1 10:15:18 UTC-0100 2008' ) 

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:

MM/DD/YYYY HH:MM:SS TZD
HH:MM:SS TZD Day Mon/DD/YYYY
Mon DD YYYY HH:MM:SS TZD
Day Mon DD HH:MM:SS TZD YYYY
Day DD Mon HH:MM:SS TZD YYYY
Mon/DD/YYYY HH:MM:SS TZD
YYYY/MM/DD HH:MM:SS TZD

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.

parse(date:String):Number 

You may also like

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.