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

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
OpenOffice 3.2 Lands Amid Critical Changes
Red Hat, IBM Firmly in KVM Virtualization Camp
Red Hat Talks Up Open Source Cloud Plans

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.


People who read this tip also read these tips:

Look for similar tips by subject:


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Search Engine Optimization: Selecting and Embedding Keywords · Are Google's Language Translation Web Services Ready for Prime Time? · Installing and Using Meeplace, the Business Review CMS
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
IBM DB2 10 for z/OS: Justifying the Upgrade · Living La Vida Colo: Choosing the Right Colocation Facility · FTC Concerns over Social Media Privacy Linger