« NeoSwiff adds Flash Player 9 support Animate Color Flex 2 Effect Updated for 2.0.1 »

December 29, 2006

ActionScript 3.0 Tip: Date Constructor

If you're doing a lot of work with dates, you might be able to make your life easier by taking full advantage of the Date constructor.

The documentation for the Date constructor lists the values that you're allowed to use when constructing a new Date instance. For the values of month and date, the valid range of values is limited to a small subset of positive integers. The month value can be 0-11 (0 = Jan, ..., 11 = Dec), and the date value can be 1-31, depending on the number of days in the month.

What the documentation doesn't tell you, however, is that you can pass pretty much any number for month and day, and still get a valid date back. This is really useful when trying to figure out some specific date values...

The following code block shows some different usages of the Date constructor, and the resulting Date that was created:

// This creates a date with a month value of 1 before January
// in 2006.  The resulting date is Dec. 1st, 2005.
var dt:Date = new Date( 2006, -1, 1 );
trace( dt ); // Thu Dec 1 00:00:00 GMT-0500 2005
				
// This tries to create January 0th, 2006, which is the last
// day of the prior month.  In this case, we get a date
// of Dec. 31, 2005.
dt = new Date( 2006, 0, 0 );
trace( dt ); // Sat Dec 31 00:00:00 GMT-0500 2005
				
// March 0th doesn't exist, so this will actually create a date
// that is the day before the 1st of March.  In this case, the date
// is the last day of February in 2008.  You can see by the output
// that this gives 29, indicating 2008 is a leap year.
dt = new Date( 2008, 2, 0 );
trace( dt ); // Fri Feb 29 00:00:00 GMT-0500 2008
				
// Create a date that is "a week ago from today"
dt = new Date();
dt.date = dt.date - 7;
trace( dt ); // Fri Dec 22 09:54:20 GMT-0500 2006

Using this concept, you can easily create a function to return the number days in any given month. To get the number of days in Feb. 2008 we simply asked for the "0th" day of Mar. 2008.

You can apply this same technique for time values as well. For example:

// The -1 for the hour gives us an hour before 0:00 on Jan 1st 2006, which is
// 11 pm on Dec 31 2005.
var dt:Date = new Date( 2006, 0, 1, -1 );
trace( dt ); // Sat Dec 31 23:00:00 GMT-0500 2005

This really isn't any new information and has been covered in the past in various other places. However, with the amount of new people coming into Flex 2 and ActionScript 3, I thought it was worth mentioning for those that might not be aware, especially because the documentation doesn't mention this at all.

I was talking to a friend this morning, and this technique made his day (pun intended!).

Happy Friday!

Tags: ,

Comments

  • This speciality of Date must be used carefully, recently I met an issue.
    If today is 2006/12/31, you want to create a Date of 2006/11/12, then use:

    var d:Date = new Date();
    d.month = 10;
    d.date = 12;
    trace(d);

    See what will happen? Exactly the Date is 2006/12/12.

    If the "date" is set first, the result will be right.

    Because Nov only has 30 days, "new Date()" is Dec which has 31 days. After the month is set to Nov, it will be "2006/11/31". Then AS3 thinks it's "2006/12/1" by the one more day, so the result is "2006/12/12".

Post a comment

Remember personal info?