I want to add months to a date in JavaScript.
For example: I am inserting date 06/01/2011
(format mm/dd/yyyy
) and now I want to add 8 months to this date. I want the result to be 02/01/2012
.
So when adding months, the year may also increase.
No date
mutatation:
With this code, the date
variable won't be changed when creating newDate
.
var date = new Date();
console.log(date);
var newDate = new Date(new Date(date).setMonth(date.getMonth() + 8));
console.log(newDate);
Corrected as of 25.06.2019:
var date = new Date();
console.log(date);
var newDate = new Date(date.setMonth(date.getMonth() + 8));
console.log(newDate);
Old From here:
var jan312009 = new Date(2009, 0, 31);
var eightMonthsFromJan312009 = new Date(jan312009.setMonth(jan312009.getMonth()+8));