Dealing with Y2K

Figure 6-9 works fine, but it has a little problem. Remember our discussion of the Y2K problem in the getYear() method of the Date object ("Writing the Date to Your Web Page" on page 26)? Different browsers deal with years differently. In some versions of Netscape, getYear() returns the year minus 1900. So if it's the year 2010, getYear() returns 110. Other versions return the full four-digit year if the year is before 1900 or after 1999. Different versions of Internet Explorer give different results for the same date as well.

The way to deal with this problem is to see whether the year returned by getYear()is less than 1000. If so, your visitor is using a browser that subtracts 1900 from the date if it's after 1899. In this case, ...

Get The Book of JavaScript, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.