JavaScript Y2K Concerns
(Appendix 4)

The year() method of the Date Object

The results that you obtain using the year() method of the date object will be different prior to the year 2000 from that you will get during the year 2000. It will also be different for different browsers.

First I will show you what it will be for the particular browser you are now using. The table below displays information on your browser plus the results of the year() method on a date in 1998 and 2001. The two dates I used for this test was July 1, 1998 and July 1, 2001 (see lessons 8 and 10 if you need to review the date object and the navigator object). I used the years 1998 and 2001 for my testing because the years 1999 and 2000 each have three digits that are the same which makes it difficult to be certain that the 2 digit year is being extracted correctly.

The table below shows the values for the year() method for the year 1998 and 2001 for different browsers. It is obvious that the results are different for different browsers.

Browser   Operating Sys   1998   2001
Netscape 3.0   win 95   98   2001
Netscape 4.04   win 95   99   2001
Netscape 4.5   win 98   98   101
Internet Explorer 4.0   win 95   98   2001
Internet Explorer 5.0   win 98   98   2001

So how do we compensate for this is our scripts. The solution is simple for browsers that have results that like shown for Netscape 4.5. All you need to do is add 1900 to either to get the 4 digit year.

Things are a little more complicated for the Internet Explorer 5 type browsers. This browser displays a 2 digit year prior to 2000 and a 4 digit year after that.

Here is a script that should work for all the browsers to return a 4 digit year.

 var today = new Date()
 var year = today.getYear()
 if(year < 1000){
   year += 1900

To display a 2 digit year, I first make the year variable a string and then use the substring() method to get the last two digits.

  year = (year+"").substring(2,4)

Here is the results for 7/1/1998 and 7/1/2001 (same two dates I used in first paragraph)

   4 and 2 digit year extracted from 7/1/1998:
   4 and 2 digit year extracted from 7/1/2001:

JavaScript version 1.2 introduced the getFullYear() method for the Date object. This method will return a correct 4 digit year for years prior to 2000 and after 2000. Netscape 4 and Internet Explorer 4 both have version 1.2 JavaScript. However, I do not use the getFullYear() method because there are still plenty of version 3 Browsers being used. This method will cause a JavaScript error in those browsers.

I updated lessons 2, 8, 9, and 11 to make all of the scripts Y2K compliant on
25 October 1999. Please send me E-mail if you discover any scripts in this tutorial that are not Y2K compliant.

[ Top of Page  |  Contents ]