The Year 2000: Introduction - Doc JavaScript | WebReference

The Year 2000: Introduction - Doc JavaScript

The Year 2000

We've all heard about the "millennium bug" (also called the "Y2K glitch"), which threatens to bring computers crashing down at the start of the year 2000. Since today's operating systems are prepared to handle the threat, most personal computers will be able to survive the rollover without any type of upgrade or intervention.

Netscape Navigator and Microsoft Internet Explorer retrieve the date from the client's operating system. Therefore, they will both recognize the 21st century without difficulty. As far as JavaScript is concerned, you must prepare your scripts so that they work in the next century. Take a look at the following script segment:

var now = new Date();
var year = now.getFullYear();

The first statement creates an instance of the Date object, reflecting the current date (because no argument is passed to the Date() constructor function). The second statement extracts the year from the new instance. The last statement then displays the current year in an alert dialog box.

The getFullYear() method returns a four-digit year for any date (current century or otherwise.) Avoid using the getYear() method, as it is not consistent across browsers. On Netscape Navigator, it returns the current year minus 1900. This year, for example, is computed as 100. On Internet Explorer, it returns the current year minus 1900 for dates at the previous century, and the full four-digit number in the current century. In other words, for years between 1970 and 1999, the value returned by the getYear() method is the year minus 1900. On Internet Explorer, however, for years greater than 1999, the method returns a four-digit year. Dates prior to 1970 are not supported in Navigator, because the browser stores dates internally as the number of milliseconds since January 1, 1970 00:00:00. In Internet Explorer, the range of dates that can be represented in a Date object (an instance of the Date object, to be exact) is approximately 285,616 years on either side of January 1, 1970.

There are two basic ways to test a script that relies on dates. You can either set the system's clock to the desired date, or create an instance of the Date object for a future date. For example, the following script displays the number 2000:

var xmas = new Date("December 25, 2000 00:00:01");
var year = xmas.getFullYear();

Click the button to see this script in action:

Note that Netscape Navigator 2.0x and Internet Explorer 3.0x differ from all other browsers. If you explicitly create an instance of the Date object for a date in the next millennium, the browser crashes. Furthermore, it always returns the current year minus 1900, regardless of the current century. For instance, the getYear() method returns 101 for the year 2001. In the next section of this article we'll show you how to overcome the different conventions. However, we won't deal with this specific behavior because Navigator 2.0x and Internet Explorer 3.0x will be long gone by the time your clock strikes midnight on December 31, 1999. Or is that January 1, 2000?

Created: February 4, 1997
Revised: June 21, 2000