DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 10.5 Converting Between DMYHMSM and Epoch Milliseconds

10.5.1 Problem

You want to convert between DMYHMSM format (days, months, years, hours, minutes, seconds, milliseconds) and Epoch milliseconds.

10.5.2 Solution

Use the getTime( ) and setTime( ) methods.

10.5.3 Discussion

Most of us are more comfortable thinking of dates and times in terms of components such as hours, days, and years than working with Epoch milliseconds or seconds. For example, it is generally more meaningful to humans to discuss the time and date 3:55 A.M., Friday, October 13, 1978 than to discuss the corresponding Epoch milliseconds value of 277124100000. However, languages such as ActionScript store times in Epoch milliseconds (or Epoch seconds) format. Therefore, it is important to be able to convert between different formats when displaying dates and times to users or when sharing dates between applications that use different formats.

When constructing a date in ActionScript, you can use the DMYHMSM approach, as follows:

// Construct a date for 3:55 AM, Friday, October 13, 1978.
myDate = new Date(1978, 9, 13, 3, 55, 0, 0);

ActionScript automatically performs the conversion and stores the date as the corresponding Epoch milliseconds value. To retrieve that value, call the getTime( ) method from the Date object:

// For Pacific Standard Time, displays: 277124100000
// The output may vary depending on your time zone.
trace(myDate.getTime(  ));

You can pass the Epoch seconds value returned by getTime( ) to another application (such as a CGI script) or use it to perform date mathematics (see Recipe 10.6).

On the other hand, you may want to set a date using the Epoch milliseconds. For example, in Recipe 10.1, the CGI script returns the current server time to Flash in Epoch seconds (which needs to be converted to milliseconds by multiplying by 1,000). Also, when performing date mathematics you may want to set a date according to Epoch milliseconds. You have two options for setting a date according to Epoch milliseconds. One choice is to pass the milliseconds value to the Date constructor as the only parameter, and the other is to pass the milliseconds value to the setTime( ) value of an existing date. Both techniques are effectively the same. The only reason to use the setTime( ) technique rather than the Date constructor is if you have assigned any custom properties to an existing date that would be lost by calling the constructor.

// Construct a new Date object for 3:55 AM, Friday, October 13, 1978. Here, we use
// the value displayed in the Output window from the preceding example.
myDate = new Date(277124100000);

// Displays: Fri Oct 13 03:55:00 GMT-0700 1978 (timezone offset may vary)
trace(myDate);

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

// Assign a custom property to the Date object.
myDate.label = "This is a special day!";

// Use the setTime(  ) method to set the date to 3:55 AM, Friday, October 13, 1978. 
// This doesn't overwrite the label property, as calling the Date constructor would.
myDate.setTime(277124100000);

// Displays: Fri Oct 13 03:55:00 GMT-0700 1978 (timezone offset may vary)
trace(myDate);

10.5.4 See Also

After assigning a value to a date using the setTime( ) method, you can use getDate( ), getMonth( ), getYear( ), getHours( ), getSeconds( ), getMinutes( ), and getMilliseconds( ) methods to retrieve the individual DMYHMSM values in the local time. To retrieve the UTC date and time, use getUTCDate( ), getUTCMonth( ), getUTCHours( ), getUTCSeconds( ), getUTCMinutes( ), and getUTCMilliseconds( ) instead. See Recipe 10.6.

    [ Team LiB ] Previous Section Next Section