13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Working with dates and times<br />

var millisecondsPerDay:int = 1000 * 60 * 60 * 24;<br />

// gets a Date one day after the start date of 1/1/1970<br />

var startTime:Date = new Date(millisecondsPerDay);<br />

Third, you can pass multiple numeric parameters to the Date() constructor. It treats those parameters as the year,<br />

month, day, hour, minute, second, and millisecond, respectively, and returns a corresponding Date object. Those input<br />

parameters are assumed to be in local time rather than UTC. The following statem<strong>en</strong>ts get a Date object set to midnight<br />

at the start of January 1, 2000, in local time:<br />

var mill<strong>en</strong>ium:Date = new Date(2000, 0, 1, 0, 0, 0, 0);<br />

Fourth, you can pass a single string parameter to the Date() constructor. It will try to parse that string into date or<br />

time compon<strong>en</strong>ts and th<strong>en</strong> return a corresponding Date object. If you use this approach, it’s a good idea to <strong>en</strong>close the<br />

Date() constructor in a try..catch block to trap any parsing errors. The Date() constructor accepts a number of<br />

differ<strong>en</strong>t string formats (which are listed in the ActionScript 3.0 Refer<strong>en</strong>ce for the Adobe Flash Platform). The<br />

following statem<strong>en</strong>t initializes a new Date object using a string value:<br />

var nextDay:Date = new Date("Mon May 1 2006 11:30:00 AM");<br />

If the Date() constructor cannot successfully parse the string parameter, it will not raise an exception. However, the<br />

resulting Date object will contain an invalid date value.<br />

Getting time unit values<br />

Flash Player 9 and later, Adobe AIR 1.0 and later<br />

You can extract the values for various units of time within a Date object using properties or methods of the Date class.<br />

Each of the following properties gives you the value of a time unit in the Date object:<br />

The fullYear property<br />

The month property, which is in a numeric format with 0 for January up to 11 for December<br />

The date property, which is the cal<strong>en</strong>dar number of the day of the month, in the range of 1 to 31<br />

The day property, which is the day of the week in numeric format, with 0 standing for Sunday<br />

The hours property, in the range of 0 to 23<br />

The minutes property<br />

The seconds property<br />

The milliseconds property<br />

In fact, the Date class gives you a number of ways to get each of these values. For example, you can get the month<br />

value of a Date object in four differ<strong>en</strong>t ways:<br />

The month property<br />

The getMonth() method<br />

The monthUTC property<br />

The getMonthUTC() method<br />

All four ways are ess<strong>en</strong>tially equival<strong>en</strong>t in terms of effici<strong>en</strong>cy, so you can use whichever approach suits your<br />

application best.<br />

The properties just listed all repres<strong>en</strong>t compon<strong>en</strong>ts of the total date value. For example, the milliseconds property<br />

will never be greater than 999, since wh<strong>en</strong> it reaches 1000 the seconds value increases by 1 and the milliseconds<br />

property resets to 0.<br />

Last updated 6/6/2012<br />

2

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!