Ian Davis Ian Davis - 1 year ago 58
Javascript Question

How to determine one year from now in Javascript

I'm trying to get one year from now's date, and it's not working.


var now = new Date();

var oneYr = new Date();
oneYr.setYear(now.getYear() + 1);

var oneMonth = new Date();
oneMonth.setMonth(now.getMonth() + 1);


one mo. = Thu Dec 22 112 15:16:01 GMT-0500 (Eastern Standard Time)

one yr. = Sun Jan 22 2012 15:16:01 GMT-0500 (Eastern Standard Time)

The year has
Dec 22 112
- ?? The month is correctly displaying
Jan 22 2012

If you want to tinker with it, http://jsbin.com/alezaj/edit#javascript,html,live. This is in Chrome and Firefox.


Answer Source

You should use getFullYear() instead of getYear(). getYear() returns the actual year minus 1900 (and so is fairly useless).