To work with dates and times in ActionScript, you use the Date object.
To use this object, you first must create an instance of the Date class, then you can access its properties and methots to manipulate dates and times in your code.
The general syntax to create a Date object is:
var obj_name:Date = new Date();
This code stores in the "
obj_name" variable the curent date and time. It uses the date and time set on the client computer (the visitor).
- Example:
var dat:Date = new Date();
trace(dat); // Tue Nov 9 09:58:18 GMT+0200 2010
The date represents a point in time. The "
new Date()" expression gets the instant in which the Date object was created. But you can also add within parentheses a series of values for the year, month, day of the month, hour, minute, second, and millisecond to create a Date object that represents any point in time.
The syntax is:
var obj_name:Date = new Date(year, month, day_month, hour, minute, second, millisecond);
- You must include at least two values, the "
year" and the "
month" are required, the other parameters will have the defaul value, 0.
The index (value) for the first month in the year (January) is 0, and 11 is the value used for the last month (December).
new Date(0) represents the 0 Unix time (January 1 1970).
- Example:
var dat:Date = new Date(2010, 0, 15);
trace(dat); // Fri Jan 15 00:00:00 GMT+0200 2010
var dat2:Date = new Date(2010, 11, 15, 18, 22, 32, 88);
trace(dat2); // Wed Dec 15 18:22:32 GMT+0200 2010
Another way to create a Date object with a certain point in time is by passing it a single String argument representing a point in time. The string must contain a year, month, and day of the month, at minimum, and can contain more.
- Examples:
var dat1:Date = new Date('1/15/2010'); // Month/Day/Year
trace(dat1); // Fri Jan 15 00:00:00 GMT+0200 2010
var dat2:Date = new Date('Sun Jan 28 2007');
trace(dat2); // Sun Jan 28 00:00:00 GMT+0200 2007
var dat3:Date = new Date('15 Oct 1976');
trace(dat3); // Fri Oct 15 00:00:00 GMT+0300 1976
var dat4:Date = new Date('Sun Jan 28 2007 14:16:12');
trace(dat4); // Sun Jan 28 15:16:12 GMT+0200 2007
If the date and time in the string doesn't have a valid time format, the Date object will return
NaN.
To check if a Date object contains a valid date, use the
isNaN() function, like this:
isNaN(obj_date.valueOf())
- This expresion returns true if the value of the argument within parentheses is a number, else, returns false.
Example:
var dat:Date = new Date('Yesterday');
trace(dat); // Invalid Date
trace(isNaN(dat.valueOf())); // true
So, if isNaN() returns true, that
dateObject is not a valid date.
The Time zone can be local time, appears as GMT, or Universal Time, abbreviated UTC (civilian time over the meridian 0, determined by astronomical measuring).
Date object Properties and Methods
Once you have a date stored in a Date variable, you can work with it and set or read any unit of time, in either UTC or in the local time zone.
ActionScript has several properties and methods for working with a Date object.
For example, if you want to access the current hour, you can use the
getHours() method (or "hours" property), and to access the day of the month, you can use the
date property (or "getDate()" method), like in the example below:
// create a Date object, for the current date time
var dat:Date = new Date();
// gets the hour
var hour:int = dat.getHours(); // or dat.hours;
// gets the day of the month
var day:int = dat.date; // or dat.getDate();
// Output the curent day and hour
trace(day); // 19
trace(hour); // 8
Also, you can use the properties and methods of Date class to change any unit in a Date object. See the following example:
// create a Date object, for a specific date and time
var dat:Date = new Date('Sun Jan 28 2007 14:16:12');
// set a new value for the day of the month
dat.date = 11; // or dat.setDate(11);
// set a new value for hour
dat.setHours(8); // or dat.hours = 8;
// Output the new date
trace(dat); // Thu Jan 11 08:16:12 GMT+0200 2007
The day of the month is set 11 (initialy 28), and the hour, 8 (initialy 14). The program automaticaly changes the day of the week according with the new settings (from "Sun" to "Thu").
Date Properties
- date - The day of the month (from 1 to 31)
- dateUTC - The day of the month (from 1 to 31), according to universal time (UTC)
- day - The day of the week (0 for Sunday, 1 for Monday, and so on)
- dayUTC - The day of the week (0 for Sunday, 1 for Monday, and so on), according to universal time (UTC)
- fullYear - The full year (a four-digit number, such as 2012)
- fullYearUTC - The full year (a four-digit number, such as 2012), (UTC)
- hours - The hour (from 0 to 23)
- hoursUTC - The hour (from 0 to 23), UTC time
- milliseconds - The milliseconds (from 0 to 999)
- millisecondsUTC - The milliseconds (from 0 to 999), according to universal time
- minutes - The minutes (from 0 to 59)
- minutesUTC - The minutes (from 0 to 59), according to universal time
- month - The month (0 for January, 1 for February, and so on)
- monthUTC - The month (0 for January, 1 for February, and so on), UTC time
- seconds - The seconds (from 0 to 59)
- secondsUTC - The seconds (an integer from 0 to 59), according to universal time
- time - The number of milliseconds since midnight January 1, 1970, universal time
- timezoneOffset - The difference, in minutes, between universal time (UTC) and the computer's local time.
Date Methods
- getDate() - Returns the day of the month based on the viewer's local time (1-31)
- getDay() - Returns the day of the week (0 for Sunday, 1 for Monday, and so on)
- getFullYear() - The full year based on the viewer's local time (four digits)
- getHours() - Returns the hour, based on the viewer's local time (0-23)
- getMilliseconds() - The number of milliseconds into the current second in Coordinated Universal Time (0-999)
- getMinutes() - Returns the minutes (from 0-59)
- getMonth() - Returns the month (0-11)
- getSeconds() - Returns the seconds (from 0 to 59)
- getTime() - The number of milliseconds since 1/1/1970
- getTimezoneOffset() - Returns the time difference (in minutes) between GMT (Greenwich Mean Time) and local time
- getUTCDate() - The day of the month in UTC, (1-31)
- getUTCDay() - Returns the day of the week, according to universal time (0-6)
- getUTCFullYear() - The full year in UTC, (four digits)
- getUTCHours() - Returns the hour, according to universal time (0-23)
- getUTCMilliseconds() - The number of milliseconds into the current second in Coordinated Universal Time (0-999)
- getUTCMinutes() - The number of minutes, in UTC (0-59)
- getUTCMonth() - The number of months into the current year in UTC (0-11)
- getUTCSeconds() - The number of seconds in Coordinated Universal Time (0-59)
- parse("string_date") - Returns the number of milliseconds since 1/1/1970 of a date sent as a parameter ("string_date") based on the viewer's local time
- setDate(day) - Sets the day of the month for an instance of the Date class (1-31)
- setFullYear(year, month, day) - Sets the year, according to local time, and returns the new time in milliseconds ("month", and "day" are optional)
- setHours(hours, minute, second, millisecond) - Sets the hours for an instance of the Date class (0-23), and returns the new time in milliseconds ("minute", "second", and "millisecond" are optional)
- setMilliseconds(ms) - Sets the milliseconds, with the value of "ms" parameter (0-999)
- setMinutes(minute, second, millisecond) - Sets the minutes in a Date object (0-59), returns the new time in milliseconds ("second", "millisecond" are optional)
- setMonth(month, day) - Sets the month for an instance of the Date class ("day" is optional), returns the new time in milliseconds
- setSeconds(second, milisecond) - Sets the seconds in a Date object ("millisecond" is optional), returns the new time in milliseconds
- setTime(milisecond) - Sets the time (in milliseconds since January 1, 1970, at midnight) for an instance of the Date object
- setUTCDate(day) - Sets the day of the month, according to universal time (1-31)
- setUTCFullYear(year, month, day) - Sets the year in Coordinated Universal Time (four digits), and returns the new time in milliseconds ("month", and "day" are optional)
- setUTCHour(hours, minute, second, millisecond) - Sets the hour, according to universal time (0-23), and returns the new time in milliseconds ("minute", "second", and "millisecond" are optional)
- setUTCMilliseconds(ms) - Sets the milliseconds, according to universal time, with the value of "ms" parameter (0-999)
- setUTCMinutes(minute, second, millisecond) - Sets the minutes, according to universal time (0-59), returns the new time in milliseconds ("second", "millisecond" are optional)
- setUTCMonth(month, day) - Sets the month, according to universal time (0-11), returns the new time in milliseconds ("day" is optional)
- setUTCSeconds(second, milisecond) - Sets the seconds, according to universal time (0-59), returns the new time in milliseconds ("millisecond" is optional)
- toLocaleString() - Returns a String representation of the day, date, time, given in local time
- toLocaleDateString() - Returns a String representation of the day and date only, and does not include the time or timezone
- toLocaleTimeString() - Returns a String representation of the time only, and does not include the day, date, year, or timezone
- toString() - Returns a String representation of the day, date, time, and timezone
- toTimeString() - Returns a String representation of the time and timezone only, and does not include the day and date
- toUTCString() - Returns a String representation of the day, date, and time in universal time (UTC)
Flash.utils.getTimer() returns the number of milliseconds since Flash Player is running.