In version 1.0, the getYear() method always returned the value of the year minus 1900. In later versions of JavaScript, starting with version 1.1, the value now depends on what the value of year is. If the year is before 2000, the return value will still be the value of the year minus 1900. If the year is 2000 or after, the complete year will be returned, unmodified. Programmers should also be aware that the setYear() method on the date object accepts both 2 digit years and 4 digits years. This can lead to obvious Y2K issues.
Any JavaScript code using the getYear() method on the Date object which was written for the JavaScript 1.0 definition of getYear() needs to be examined. This code may need to be modified to assure compatibility between JavaScript 1.0 and later versions. From a client (i.e. browser) standpoint, this will depend on the requirement your application has on how far back in terms of versions of browser that must be supported. See the table in the next section for a list of versions of Netscape and Internet Explorer that support these versions.
For servers running JavaScript code, it is recommended that the server should be upgraded to a version that supports JavaScript 1.2 or later, which makes the solution much easier to implement.
setYear():
As stated previously, this method accepts both two and four digit years as input. In order to avoid potential Y2K problems, you should modify your code, if necessary, to always pass a four digit year to the setYear() method on the Date object. Code should eventually be migrated to use the new method setFullYear(). This change is the same regardless of weather the code is run on the server or the client.
getYear():
For client-side JavaScript, consult the table below to see affected systems:
|---------------+---------------+---------------+--------------->
|NS Version |IE Version |JS Version |getYear() |
| | | |return for 2001|
|---------------+---------------+---------------+--------------->
>---------------|
|getFullYear |
|supported? |
|(NS/IE) |
>---------------|
|---------------+---------------+---------------+--------------->
|<2.0 |<3.0 |NONE |-- |
|---------------+---------------+---------------+--------------->
>---------------|
|-- |
>---------------|
|---------------+---------------+---------------+--------------->
|2.0 |3.0 |1.0 |101 |
|---------------+---------------+---------------+--------------->
>---------------|
|No/No |
>---------------|
|---------------+---------------+---------------+--------------->
|3.0 |?.? |1.1 |2001 |
|---------------+---------------+---------------+--------------->
>---------------|
|No/Yes |
>---------------|
|---------------+---------------+---------------+--------------->
|4.04 |4.0 |1.2 |2001 |
|---------------+---------------+---------------+--------------->
>---------------|
|Yes/Yes |
>---------------|
If your application client requirements are such that you only need to support browser versions that utilize JavaScript 1.1 or later, it is recommended that you simply migrate from using getYear( ) to getFullYear() which is a method that was introduced in version 1.1 and always returns a valid four digit year. If, however, you must support browser versions that only utilize version 1.0, it is recommended that you use the following code in your webpages that use the getYear() method. By making these changes, you will be making your code tolerant of both JavaScript 1.0 and later versions. After using the following code segment to define a "get4Year" function, you then need to change your code that uses "getYear" to use this function and you can be assured you will get the correct 4 digit year back regardless of what version of JavaScript is running on your client.
When this code is added to webpages, a call can be made to get4Year() passing it a date object. get4Year() will return a 4 digit year correctly, without the programmer worrying about which version of JavaScript is supported in the current browser. This code also migrates to getFullYear() as getYear() has become depricated and may not exist in future versions of JavaScript.
On servers running JavaScript code, the servers should be upgraded to a version that supports JavaScript 1.2 and all getYear() calls should be migrated to getFullYear() calls. If this is not feasible, then you can use the "get4Year" solution defined above for the version of JavaScript that is supported on your server and then use get4Year in your application code to assure always getting a valid 4 digit year.