How to determine one year from now in Javascript
Asked Answered
P

7

196

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

JS:

var now = new Date();

var oneYr = new Date();
oneYr.setYear(now.getYear() + 1);
$("#yearFromNow").append(oneYr.toString());

var oneMonth = new Date();
oneMonth.setMonth(now.getMonth() + 1);
$("#monthFromNow").append(oneMonth.toString());

Output:

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.

Thanks!

Popery answered 22/12, 2011 at 20:17 Comment(0)
R
180

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

Thus a date marking exactly one year from the present moment would be:

var oneYearFromNow = new Date();
oneYearFromNow.setFullYear(oneYearFromNow.getFullYear() + 1);

Note that the date will be adjusted if you do that on February 29.

Similarly, you can get a date that's a month from now via getMonth() and setMonth(). You don't have to worry about "rolling over" from the current year into the next year if you do it in December; the date will be adjusted automatically. Same goes for day-of-month via getDate() and setDate().

Rule answered 22/12, 2011 at 20:18 Comment(2)
For anyone wondering how it deals with leap years, I did this in a Firefox console ... >> x = new Date('2000-02-29') -> Date 2000-02-29T00:00:00.000Z >> x.setYear(2001) -> 983404800000 >> x.toLocaleFormat('%d-%b-%Y') -> "01-Mar-2001"Kathrinekathryn
Isn't this hilarious? To get a date one year from now in javascript one needs to write: new Date(new Date().setFullYear(new Date().getFullYear() + 1))Vestry
S
294

This will create a Date exactly one year in the future with just one line. First we get the fullYear from a new Date, increment it, set that as the year of a new Date. You might think we'd be done there, but if we stopped it would return a timestamp, not a Date object so we wrap the whole thing in a Date constructor.

new Date(new Date().setFullYear(new Date().getFullYear() + 1))
Sumerlin answered 14/3, 2015 at 17:32 Comment(5)
setYear() is deprecated, setFullYear() should be used instead.Sloven
One liner with no intermediate variables to allocate! Excellent!Underprop
this will not handle the hours, mins and seconds, it will be set to current timeCatabolism
If you call this exactly at the end/begin of the year, different calls of new Date() can return different years, increasing by (almost) two years or none!?Mcquoid
maybe not the most efficientCharry
R
180

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

Thus a date marking exactly one year from the present moment would be:

var oneYearFromNow = new Date();
oneYearFromNow.setFullYear(oneYearFromNow.getFullYear() + 1);

Note that the date will be adjusted if you do that on February 29.

Similarly, you can get a date that's a month from now via getMonth() and setMonth(). You don't have to worry about "rolling over" from the current year into the next year if you do it in December; the date will be adjusted automatically. Same goes for day-of-month via getDate() and setDate().

Rule answered 22/12, 2011 at 20:18 Comment(2)
For anyone wondering how it deals with leap years, I did this in a Firefox console ... >> x = new Date('2000-02-29') -> Date 2000-02-29T00:00:00.000Z >> x.setYear(2001) -> 983404800000 >> x.toLocaleFormat('%d-%b-%Y') -> "01-Mar-2001"Kathrinekathryn
Isn't this hilarious? To get a date one year from now in javascript one needs to write: new Date(new Date().setFullYear(new Date().getFullYear() + 1))Vestry
P
42

As setYear() is deprecated, correct variant is:

// plus 1 year
new Date().setFullYear(new Date().getFullYear() + 1)
// plus 1 month
new Date().setMonth(new Date().getMonth() + 1)
// plus 1 day
new Date().setDate(new Date().getDate() + 1)

All examples return Unix timestamp, if you want to get Date object - just wrap it with another new Date(...)

Prevaricate answered 4/2, 2016 at 5:59 Comment(0)
B
9

Use this:

var startDate = new Date();
    startDate.setFullYear(startDate.getFullYear() - 1);
Breast answered 2/5, 2019 at 6:32 Comment(0)
F
7

Using some of the answers on this page and here, I came up with my own answer as none of these answers fully solved it for me.

Here is crux of it

var startDate = "27 Apr 2017";
var numOfYears = 1;
var expireDate = new Date(startDate);
expireDate.setFullYear(expireDate.getFullYear() + numOfYears);
expireDate.setDate(expireDate.getDate() -1);

And here a a JSFiddle that has a working example: https://jsfiddle.net/wavesailor/g9a6qqq5/

Fieldwork answered 27/4, 2017 at 18:37 Comment(1)
whats the expireDate.getDate() - 1 for?Kowtow
T
2

Use setFullyear as others have posted but be aware this returns a timestamp value not a date object. It is also a good candidate imho to add functionality via the prototype. This leads us to the following pattern:

Date.prototype.addYears = function(n) {
    var now = new Date();
    return new Date(now.setFullYear(now.getFullYear() + n));
};

console.log('Year from now is', new Date().addYears(1));
Therapeutic answered 7/9, 2016 at 10:55 Comment(0)
C
0

In very simple way. use this code.

// define function 
function nextYearDate(date1) {
    var date2 = new Date(date1);
    var date3 = date2.setDate(date2.getDate() - 1);
    var date = new Date(date3);
    var day = date.getDate();
    var month = date.getMonth()+1;
    var year = date.getFullYear()+1;
    var newdate = year + '-' + (month < 10 ? '0' : '') + month + '-' + (day < 10 ? '0' : '') + day;
    $("#next_date").val(newdate);
}
// call function.
<input type="date" name="current_date" id="current_date" value="" onblur="nextYearDate(this.value);" />

<input type="date" name="next_date" id="next_date" value="" onblur="nextYearDate(this.value);" />
Copra answered 1/12, 2017 at 8:33 Comment(1)
Why on earth is JQuery rearing its ugly head here? JQuery != JSKowtow

© 2022 - 2024 — McMap. All rights reserved.