How to add months to a date in JavaScript?

Kanak Vaghela picture Kanak Vaghela · Apr 13, 2011 · Viewed 447.2k times · Source

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.

Answer

Daniel Protopopov picture Daniel Protopopov · Apr 13, 2011

Corrected as of 25.06.2019:

var newDate = new Date(date.setMonth(date.getMonth()+8));

Old From here:

var jan312009 = new Date(2009, 0, 31);
var eightMonthsFromJan312009  = jan312009.setMonth(jan312009.getMonth()+8);