home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


JavaScript: The Definitive Guide

Previous Chapter 21
JavaScript Reference
Next
 

Date.getYear() Method

Name

Date.getYear() Method---return the year field of a Date

Availability

Navigator 2.0, Internet Explorer 3.0

Synopsis

date.getYear()

Arguments

none

Returns

The year field of the specified Date object date.

Description

getYear() returns the year field of a specified Date object. The format that this year is returned in requires additional explanation, however. In Navigator 2.0 and 3.0, the return return value of this method is the year minus 1900 for dates between the years 1900 and 1999. For example, if date represents a date in 1997, then the return value would be 97. On the other hand, for dates in years prior to 1900 or after 1999, getYear() returns the year itself in Navigator 2.0 and 3.0. For example, if date represents a date in 2000, the method returns 2000 on Navigator platforms.

Internet Explorer 3.0 always returns the year minus 1900, however, so getYear() on this platform would return 85 to represent 1985, 100 to represent the year 2000 and 110 to represent 2010. IE 3.0 cannot represent years prior to 1970, so these return values are never negative numbers.

To work around these strange and incompatible return values, you should replace getYear() with a function like the following:

function getFullYear(d) returns the correct year for any year after 1000
{
    var y = d.getYear();
    if (y < 1000) y += 1900;
    return y;
}

Bugs

The disparity in return values between dates in the twentieth and twenty-first centuries in Navigator is bizarre, and, if not carefully taken into account may be the source of "millennium bugs" in your code. The incompatibility between platforms makes this method especially annoying to use.


Previous Home Next
Date.getTimezoneOffset() Book Index Date.parse()

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell