178

I have this javascript for automatically setting a date filter to the first and last day of the previous month:

$(document).ready(function () {
    $("#DateFrom").datepicker({ dateFormat: 'dd/mm/yy' });
    $("#DateTo").datepicker({ dateFormat: 'dd/mm/yy' });

    var now = new Date();
    var firstDayPrevMonth = new Date(now.getYear(), now.getMonth() - 1, 1);
    var firstDayThisMonth = new Date(now.getYear(), now.getMonth(), 1);
    var lastDayPrevMonth = new Date(firstDayThisMonth - 1);

    $("#DateFrom").datepicker("setDate", firstDayPrevMonth);
    $("#DateTo").datepicker("setDate", lastDayPrevMonth);
}); 

BUT now.getYear() is returning 111 instead of the expected 2011. Is there something obvious I've missed?

JK.
  • 21,477
  • 35
  • 135
  • 214

3 Answers3

301

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getYear

getYear is no longer used and has been replaced by the getFullYear method.

The getYear method returns the year minus 1900; thus:

  • For years greater than or equal to 2000, the value returned by getYear is 100 or greater. For example, if the year is 2026, getYear returns 126.
  • For years between and including 1900 and 1999, the value returned by getYear is between 0 and 99. For example, if the year is 1976, getYear returns 76.
  • For years less than 1900, the value returned by getYear is less than 0. For example, if the year is 1800, getYear returns -100.
  • To take into account years before and after 2000, you should use getFullYear instead of getYear so that the year is specified in full.
Community
  • 1
  • 1
deceze
  • 510,633
  • 85
  • 743
  • 889
  • 4
    I can't believe a copy-n-paste answer from the manual got 31 upvotes (at the time of writing). Guess it shows how common this issue is and that nobody RTFM... ;o) – deceze May 14 '12 at 01:51
  • @Mark actually I was using this SO question http://stackoverflow.com/questions/605113/find-first-day-of-previous-month-in-javascript that was wrong originally (but it has since been updated to use getFullYear()). I would never read w3schools for anything :) – JK. Jul 24 '12 at 21:33
  • @JK. To be entirely honest, I suspect it was probably me who read W3Schools. Every now and again I delve into there and usually begin regretting it fairly quickly. – Mark Henderson Jul 24 '12 at 21:57
  • Wow the millenium bug is real, it took 13 years, but this is the first time I see a real life example... I always used getFullYear before but I wondered what this was (saw it as an autocomplete option). – Erik Nijland Jul 10 '13 at 13:44
34

In order to comply with boneheaded precedent, getYear() returns the number of years since 1900.

Instead, you should call getFullYear(), which returns the actual year.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • @StevenLu: See the horrors of the original Java Date API – SLaks May 29 '15 at 00:25
  • 2
    Wow. Deprecated in the Java API in 1997. Not just 1997, *February* 1997... before 1997 really even got underway... You know, I thought Javascript wasn't really connected to Java... – Steven Lu May 29 '15 at 03:24
7

From what I've read on Mozilla's JS pages, getYear is deprecated. As pointed out many times, getFullYear() is the way to go. If you're really wanting to use getYear() add 1900 to it.

var now = new Date(),
    year = now.getYear() + 1900;
Pazuzu156
  • 679
  • 6
  • 13