O'Reilly logo

ActionScript 3.0 Cookbook by Joey Lott, Darron Schall, Keith Peters

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Finding the Current Date and Time

Problem

You want to know the current date and time.

Solution

Create a new date by using the Date() constructor with no parameters. Alternatively, use a CGI script or any other server-side program to return the server time and create a new Date object from that value.

Discussion

The date and time that ActionScript calculates on its own is based on the client computer’s date and time settings. Therefore, if the user’s computer has the incorrect time, so will the Date object. With that caveat in mind, you can retrieve the current client-side date and time by creating a new Date object by using a constructor without parameters, as follows:

// Create a new Date object.
var current:Date = new Date();

// Displays client-side date and time.
trace(current);

If you have an Internet connection, the Flash movie attempts to retrieve the date and time from a server. This technique can ensure more accurate dates and times. Although the server’s time settings might be inaccurate, at least the time will be consistent for all clients.

The basic process when reading the time from a server is as follows:

  1. Create a CGI script on the web server that outputs the number of seconds since midnight of January 1, 1970 (the epoch).

  2. Use a flash.net.URLLoader object from ActionScript to load the epoch seconds.

  3. Convert the loaded seconds from a string to a number, multiply by 1,000, and construct a new Date object by passing the value to the constructor.

PHP is a scripting language that can ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required