Adobe FLEX 2-PROGRAMMING ACTIONSCRIPT 3.0 Manual page 200

Programming actionscript 3.0
Table of Contents

Advertisement

Now it is easy to perform date arithmetic using standard time units. The following code sets a
date value to one hour from the current time using the
and
methods:
getTime()
setTime()
var oneHourFromNow:Date = new Date();
oneHourFromNow.setTime(oneHourFromNow.getTime() + millisecondsPerHour);
Another way to set a date value is to create a new Date object using a single milliseconds
parameter. For example, the following code adds 30 days to one date to calculate another:
// sets the invoice date to today's date
var invoiceDate:Date = new Date();
// adds 30 days to get the due date
var dueDate:Date = new Date(invoiceDate.getTime() + (30 *
millisecondsPerDay));
Next, the
constant is multiplied by 30 to represent 30 days' time and
millisecondsPerDay
the result is added to the
value and used to set the
value.
invoiceDate
dueDate
Converting between time zones
Date and time arithmetic comes in handy when you want to convert dates from one time
zone to another. So does the
method, which returns the value in
getTimezoneOffset()
minutes by which the Date object's time zone differs from UTC. It returns a value in minutes
because not all time zones are set to even-hour increments—some have half-hour offsets from
neighboring zones.
The following example uses the time zone offset to convert a date from local time to UTC. It
does the conversion by first calculating the time zone value in milliseconds and then adjusting
the Date value by that amount:
// creates a Date in local time
var nextDay:Date = new Date("Mon May 1 2006 11:30:00 AM");
// converts the Date to UTC by adding or subtracting the time zone offset
var offsetMilliseconds:Number = nextDay.getTimezoneOffset() * 60 * 1000;
nextDay.setTime(nextDay.getTime() + offsetMilliseconds);
200
Working with Dates and Times

Advertisement

Table of Contents
loading

This manual is also suitable for:

Flex

Table of Contents