Skip to content Skip to sidebar Skip to footer

Javascript Date Getyear() Returns Different Result Between Ie And Firefox, How To Approach This?

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 =

Solution 1:

Use getFullYear() instead of getYear().

Solution 2:

try to use getFullYear() instead getYear

Solution 3:

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

Solution 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 = newDate();
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.

Solution 5:

Use date.getFullYear();

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

Post a Comment for "Javascript Date Getyear() Returns Different Result Between Ie And Firefox, How To Approach This?"