8

Apparently javascript date object's method getYear() returns different result between IE8 and Firefox3.6 (I have those 2 on my machine, not sure other browser or version)

Date d = new Date();
alert(d.getYear());

FF3.6 ==> 111 (year since 1900? i guess)
IE8 ===> 2011

I have been only testing on Firefox and now my Javascript code that adjust returned value of getYear() is now giving me 3911 because of my coding.

var modified = d.getYear() + 1900

On Firefox it return 2011. But if I apply this approach on IE8, it return 3911.

I could add logic to distinguish IE and Firefox but I don't want to add such if/else everywhere in my code wherever there are browser dependent parts like this. Is there other way to approach this problem?

var browserName=navigator.appName; 

if (browserName=="Netscape") { 
   var modified = d.getYear() + 1900
}
else if(browserName=="Microsoft Internet Explorer") { 
   var modified = d.getYear();
}
Meow
  • 18,371
  • 52
  • 136
  • 180
  • possible duplicate of [Why does Javascript getYear() return 108?](http://stackoverflow.com/questions/98124/why-does-javascript-getyear-return-108) – Caspar Kleijne Apr 23 '11 at 08:29
  • *"year since 1900? i guess"* Why guess? The specification is freely available: http://www.ecma-international.org/publications/standards/Ecma-262.htm – T.J. Crowder Apr 23 '11 at 08:36
  • http://msdn.microsoft.com/en-us/library/windows/apps/29y2w2x3%28v=VS.94%29.aspx says that getYear is deprecated. If it's deprecated, why did they mess with it?! – JoelFan Jan 13 '12 at 01:07

5 Answers5

20

Use getFullYear() instead of getYear().

Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34
psx
  • 4,040
  • 6
  • 30
  • 59
  • 4
    Firefox is doing *exactly* what the specification says it should do. – T.J. Crowder Apr 23 '11 at 08:31
  • 1
    `getYear` is y2k compliant since the result isn't not limited to two digits. For example, it will return 112 for 2012, not 12. It's a unixy thing. – ikegami Jan 13 '12 at 18:43
8

try to use getFullYear() instead getYear

Naren Sisodiya
  • 7,158
  • 2
  • 24
  • 35
4

If IE8 is giving you 2011, It's a bug in IE8 (and earlier, see update below). getYear is defined in the specification (Section B.2.4) as being:

  1. Let t be this time value.
  2. If t is NaN, return NaN.
  3. Return YearFromTime(LocalTime(t)) − 1900.

Thus right now, 111 is the correct value. That definition is unchanged from the 3rd edition, so we're talking ~12 years of specified behavior.

As others have said, use getFullYear to get a more useful value, but that's an IE8 bug if it's truly as you say (I don't have IE8 handy to check).


Update: Well I'll be. Just tried it, and Microsoft did get it wrong. IE6, IE7, and IE8 all say "2011". The good news is they've finally fixed it, IE9 says "111" as it should. You can try it in your browser here: http://jsbin.com/ofuyi3

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
4

Don't rely on product versions when you don't have to. Instead, rely on the difference you want to correct itself. If you wanted getYear's correct value, you could get it using

Date d = new Date();
var year = d.getYear();
if (year < 1900) {  // Should always be true, but isn't in older IE.
   year += 1900;
}

I realise people have suggested a better way of getting the result, but I thought the actual question was worth answering.

ikegami
  • 367,544
  • 15
  • 269
  • 518
2

Use date.getFullYear();

blah.. gotta answer with at least 30 characters...

RobG
  • 142,382
  • 31
  • 172
  • 209