July 7, 2000 - getFullYear() vs. getYear() | WebReference

July 7, 2000 - getFullYear() vs. getYear()

Yehuda Shiran July 7, 2000
getFullYear() vs. getYear()
Tips: July 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

Seems like the dilemma whether to use getFullYear() or getYear() is still not settled. As far as functionality is concerned, getFullYear() should be always used. It returns the full four-digit year, no matter if the date is before 2000, in 2000, or after 2000. Equally important is the fact that both browser return the same value for the getFullYear() method. The only caveat is the getFullYear() method has been added to the Date object some time later than the getYear() method. Therefore, some old browsers cannot interpret getFullYear(). But what's the problem with getYear()? It behaves differently on Internet Explorer than on Netscape Navigator. The following table summarizes the differences, by showing the returned values for two different years, 1998 and 2000:

year

IE

NS

1998

98

98

2000

2000

100

Obviously, not a pleasant situation as far as browser compatibility is concerned. You have to detect the browser and act accordingly. Also, this behavior is consistent across all JavaScript versions, from 1.1 to 1.3.

But, if you check again the getFullYear() method, you will find that it is supported by JavaScript 1.1 and above on both Internet Explorer and Netscape Navigator. Netscape's original documentation stating that this method is new in JavaScript 1.3 is no longer correct. In summary, I recommend to use getFullYear(), as most browsers today are capable of handling JavaScript 1.1 and above. For more on dates, go to Column 2, Mastering JavaScript Dates. For more on the different versions of JavaScript, go to Column 25, JavaScript 1.3 Review, Part I, and Column 26, JavaScript 1.3 Review, Part II.