JavaScript 1.3 Overview, Part I: Date Object - Doc JavaScript | WebReference

JavaScript 1.3 Overview, Part I: Date Object - Doc JavaScript

Date Object

The Date object has changed a lot in JavaScript 1.3. As mentioned earlier in this column, the motivation behind JavaScript 1.3's changes is conformance to ECMA and world-wide globalization. JavaScript 1.2's implementation of the Date object suffered from two deficiencies. The first one is that it depended on platform-specific date facilities and behavior. The second one is that it was based on local time. JavaScript 1.3 remedies both. It removes all previous platform dependencies, and provides a uniform behavior across platforms. JavaScript 1.3 also supports several new Coordinated Universal Time (UTC) methods, in addition to JavaScript 1.2's local time methods. UTC, also known as Greenwich Mean Time (GMT), is set by the World Time Standard. The local time is the time as seen by the computer running the JavaScript script.

As in JavaScript 1.2, date is measured in milliseconds since midnight 01 January, 1970 UTC. One day is 86,400,000 milliseconds. The Date object can hold a date that is -100,000,000 days to 100,000,000 days relative to 01 January, 1970 UTC. JavaScript 1.3 adds the UTC method to the Date constructor. Date.UTC is different than the Date constructor in that it uses the UTC instead of the local time. Another difference is that Date.UTC returns a numeric value that is equal to the number of milliseconds since midnight 01 January, 1970 UTC. The Date constructor, on the other hand, creates a Date object. Here is the full syntax for computing the number of UTC milliseconds:

Date.UTC(<year>, <month>, <day>[, <hour>[, <min>[, <second>[, <millisecond>]]]]);

You should specify a full year in the invocation above, 1998 for example. If you specify a two-digit year number, the methods adds 1900 to it. If a parameter value is outside of the expected range, the UTC method updates the other parameters to accommodate your specific requirements. If you specify, for example, 73 minutes, the hour will be updated by one and 13 will be used for hour.

Notice the difference between the Date object and the Date constructor. The first one is a built-in object that can be queried for different date-related information. For example, Date.UTC returns the number of milliseconds from midnight, January 1, 1970. The second Date is the constructor by which you can create your date objects. For example, the statement myWedding = new Date(); creates a new date object, myWedding. If, for example, Today is a date object that holds the current date, we can subtract Today from myWedding to find out how many years am I married.

JavaScript 1.3 enhances the Date constructor, by adding milliseconds specification option. JavaScript 1.2 supported specification of years, months, days, hours, and minutes, but not milliseconds. The hour, minute, second, and millisecond specification is optional:

Date(<year>, <month>, <day>[, <hour>[, <min>[, <second>[, <millisecond>]]]]);

Created: September 14, 1998
Revised: September 14, 1998