How to determine a year later in Javascript

I am trying to get a year from the date and it is 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) 

In the year there are Dec 22 112 - ?? The month correctly displays Jan 22 2012 .

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

Thank!

+86
javascript
Dec 22 '11 at 20:17
source share
8 answers

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

+57
Dec 22 '11 at 20:18
source share

This will create a Date exactly one year in the future with just one line. First we get fullYear from a new Date , increment it, set new Date as the year. You might think that we will do this, but if we stop, it will return a timestamp, not a Date object, so we will end all of this in the Date constructor.

 new Date(new Date().setFullYear(new Date().getFullYear() + 1)) 
+171
Mar 14 '15 at 17:32
source share

Since setYear() deprecated, the correct option 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 a Unix timestamp if you want to get a Date object - just wrap it with another new Date(...)

+27
Feb 04 '16 at 5:59
source share

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

Here is the gist

 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 is the JSFiddle that has a working example: https://jsfiddle.net/wavesailor/g9a6qqq5/

+6
Apr 27 '17 at 18:37
source share

Use this:

 var startDate = new Date(); startDate.setFullYear(startDate.getFullYear() - 1); 
+2
May 2 '19 at 6:32
source share

Use setFullyear like the others, but you know that it returns a timestamp value, not a date object. It is also a good imho candidate for adding functionality through a 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)); 
+1
Sep 07 '16 at 10:55 on
source share

I came up with a direct solution that takes into account leap years.

For example:

 var originalDate = new Date('February 29th, 2016 12:00:00'); var newDate = dateAdd(originalDate, 1, 'years'); // => 'February 28th, 2017 

It also works with other time units such as days, weeks, months, etc.

0
Jun 25 '17 at 14:22
source share

Very simple. 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);" /> 
0
Dec 01 '17 at 8:33
source share



All Articles