Why does Javascript getYear () return 108?

Why does this javascript return 108 instead of 2008? Does he get the day and month correctly, but not the year?

myDate = new Date(); year = myDate.getYear(); 

year = 108?

+89
javascript date
Sep 18 '08 at 23:53
source share
14 answers

This is Y2K , only years since 1900 are counted.

There are currently potential compatibility issues that getYear() deprecated in favor of getFullYear() - from quirksmode :

To make this question even more complex, date.getYear () is now deprecated, and you should use date.getFullYear (), which in turn is not supported by older browsers. However, if it works, it should always give a full year, i.e. 2000 instead of 100.

The following browsers have the following two methods:

 * The year according to getYear(): 108 * The year according to getFullYear(): 2008 

There are also differences in the implementation between Internet Explorer and Firefox, since the IE implementation of getYear() been changed to behave like getFullYear() from IBM :

In the ECMAScript specification, getYear returns the year minus 1900, originally intended to return "98" in 1998. getYear is deprecated in ECMAScript version 3 and is replaced by getFullYear ().

Internet Explorer changed getYear () to work as getFullYear () and made it compatible with Y2k, while Mozilla retained the standard behavior.

+121
Sep 18 '08 at 23:58
source share

Since getFullYear does not work in older browsers, you can use something like this:

 Date.prototype.getRealYear = function() { if(this.getFullYear) return this.getFullYear(); else return this.getYear() + 1900; }; 

Javascript prototype can be used to extend existing objects, similar to C # extension methods. Now we can just do this:

 var myDate = new Date(); myDate.getRealYear(); // Outputs 2008 
+23
Sep 19 '08 at 0:12
source share

Check documents. This is not a Y2K problem - this is the lack of a problem in 2000! This decision was originally made in C and was copied to Perl, obviously JavaScript and possibly several other languages. Once upon a time, it seemed that it would be desirable to use double-digit years, but, great, whoever developed this interface, there was not enough forethought to understand that they need to think about what will happen in 2000 and beyond, therefore instead in order to simply provide the last two digits, they provided the number of years since 1900. You could use two numbers if you were in a hurry or want to be risky. Or, if you want your program to continue to work, you can add 100 to the result and use the full four-digit years.

I remember the first time I did manipulations with Perl. Oddly enough, I read the documents . This is apparently not an ordinary thing. After a year or two, I was called to the office on December 31, 1999 to correct a mistake that was discovered at the last minute in some kind of Perl contract code in which I never had anything to do. It was this exact problem: the standard call by date returned from 1900, and programmers viewed it as a two-digit year. (They assumed that they would receive “00” in 2000.) As a young inexperienced programmer, it seemed to me that we paid the same amount for “professional” work, and these people did not even bother to read the documentation. This was the beginning of many years of disappointment; now i am old and cynical. :)

In 2000, the annual YAPC Perl conference was named "YAPC 19100" after this often-asked non-error.

Currently, at least in the Perl world, it makes sense to use a standard date processing module that uses real four-digit years. Not sure what might be available for JavaScript.

+9
Sep 19 '08 at 0:42
source share

He must return the number of years since 1900.

+6
Sep 18 '08 at 23:55
source share

use date.getFullYear() .

This (as indicated elsewhere) is a Y2K thing. Netscape (written before 2000) originally returned, for example 98 from getYear() . Instead of returning to 00 instead, he returned 100 for the year 2000. Then other browsers appeared and did it differently, and everyone was unhappy that incompatibility reigned.

Browsers later supported getFullYear as the standard method for returning all year.

+4
Sep 18 '08 at 23:55
source share

This question is so old that it makes me cry with nostalgia for dotcom days!

That's right, Date.getYear () returns the number of years since 1900, just like Perl localtime (). I wonder why the language created in the 1990s would not take into account the centennial turnover, but what can I say? You should have been there. At one time it was a kind of feeling (for example, pets.com).

Until 2000, it might be tempting to fix this error by adding "19" to the result of getYear (), resulting in a "19100 error . " Others have already answered this question quite well (add 1900 to the result of getDate ()).

Is the book you're reading about JavaScript a bit old?

Thanks for the explosion from the past!

+2
Sep 19 '08 at 0:22
source share

You should, as indicated, never use getYear() , but instead use getFullYear() .

However, the story is not as simple as "IE implements getYear() as getFullYear() . Opera and IE getYear() view getYear() as getYear() for dates before 2000, but will refer to it as getFullYear() for dates after 2000, while webkit and Firefox stick to old behavior

This outputs 99 in all browsers:

 javascript:alert(new Date(917823600000).getYear()); 

This outputs 108 in FF / WebKit and 2008 in Opera / IE:

 javascript:alert(new Date().getYear()); 
+2
Sep 19 '08 at 9:46
source share

This is stupid. These are dates before the pre-Y2K days , and now simply returns the number of years since 1900 for hereditary reasons. Use getFullYear () to get the actual year.

+1
Sep 18 '08 at 23:58
source share

I am using date.getUTCFullYear() ; no problem.

+1
Aug 10 '10 at 9:26 a.m.
source share

The number you get is the number of years since 1900. Do not ask me why ..

0
Sep 18 '08 at 23:55
source share

he returns a 4-digit year - 1900, which may have been cool 9+ years ago, but is now quite backward. Java java.util.Date also does this.

0
Sep 18 '08 at 23:55
source share

As others have said, it returns the number of years since 1900. The reason for this is that when JavaScript was invented in the mid-90s, this behavior was convenient and compatible with APIs with dates in other languages, In particular, C. And, of course, after the API was installed, they could not change it for backward compatibility reasons.

0
Sep 18 '08 at 23:58
source share

BTW, different browsers may return different results, so it’s better to skip this function altogether and always use getFullYear ().

0
Sep 19 '08 at 0:03
source share

var date_object = new Date (); var year = date_object.getYear (); if (year <2000) {year = year + 1900; } // u will get the full year ....

0
Nov 25 '08 at 7:03
source share



All Articles