Recipe 10.1 Finding the Current Date and Time

10.1.1 Problem

You want to know the current date and time.

10.1.2 Solution

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

10.1.3 Discussion

The date and time that ActionScript can calculate 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 Flash movie. With that caveat in mind, you can retrieve the current client-side date and time by creating a new Date object using the Date( ) constructor with no parameters, as follows:

// Create a new Date object.
today = new Date(  );

// Displays client-side date and time

Avoid the temptation to name your date variable date. Unlike in JavaScript, ActionScript identifiers (including variable and class names) are case-insensitive. Date is the name of the constructor function for the Date class; therefore, naming a variable date disables the Date constructor.

If an active Internet connection is available, the Flash movie can try 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 client movies.

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

  1. Create a CGI script on the web server that outputs a name/value pair, in which the value is the number of seconds since midnight January 1, 1970 (the Epoch).

  2. Within the Flash movie, use a LoadVars object (or the loadVariables( ) method) to load the Epoch seconds.

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

PHP is a scripting language that can be found on a large number of web hosts. It is quite simple to create a PHP page to output the current time and date as the number of seconds since the Epoch. All you need to do is create a PHP document with the following content and upload it to the server:

now=<?php echo time(  );?>

If you don't have PHP on your server, or if you are simply more comfortable with Perl (another language that is almost universally available on web servers), then here is a Perl script that outputs the number of seconds since the Epoch:

print "Content-type:text/plain\n\n";
print "now=";
print time;

There are a few tips to keep in mind when setting up this script on your server:

  • The first line indicates where the Perl interpreter can be found. The value given in the example is fairly universal. However, contact your web server administrator if you encounter problems.

  • Many servers disable the remote execution of scripts without particular file extensions. The .cgi extension is commonly allowed. Try naming the script getDate.cgi.

  • Most web servers limit CGI access to specific directories. Normally, these directories are found in the account root directory (or within the web root of the account), and are named either cgi or cgi-bin. Make sure you save the script in the correct directory.

  • On Unix servers, your CGI script must have its permissions set to 755. Most FTP programs allow you to change permissions. If you are working from a shell script, use the following command:

    chmod 755 filename

Within the Flash document, you need to load the time value from the server. The best way to accomplish this is to use a LoadVars object. The LoadVars class was introduced in Flash MX. For compatibility with earlier Flash Player versions, use the MovieClip.loadVariables( ) method instead.

If you pass the Date( ) constructor a single value, ActionScript interprets it as the number of milliseconds since the Epoch and creates a new Date object that corresponds to that value. Therefore, you must multiply the value returned by the script (which is in seconds) by 1,000.

// Create a LoadVars object.
lv = new LoadVars(  );

// Use the load(  ) method to call the CGI script on the server. If using PHP, this
// should point to the appropriate PHP page, such as getDate.php, instead.

// onLoad(  ) is invoked automatically when the server returns a response.
lv.onLoad = function (  ) {

  // Create a Date object by passing (seconds * 1000) to the Date(  ) constructor. The
  // value returned by the server is this.nameOfVariable, in this case, 
  // Convert the string value to a number before multiplying it by 1000.
  var serverDate = new Date(Number( * 1000);

  // Display the date and time returned by the server (in this case, with time zone
  // offsets for the user's computer settings).

The date is always stored in ActionScript as the milliseconds since the Epoch, but it is always displayed with proper offsets based on the user's local time zone setting (unless you specifically use the UTC methods). So if the user's computer has the incorrect time zone setting, the display might be incorrect. However, the actual date (as stored in Epoch milliseconds) is still correct.

10.1.4 See Also

If your server's time is not reliable or accurate enough for your needs, there are many existing date and time servers on the Internet from which you can retrieve reasonably accurate date and time information. For example, see For details on synchronizing time via the Network Time Protocol, see and Recipe 10.7.

    Part I: Local Recipes
    Part II: Remote Recipes