Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add months to a date in JavaScript? [duplicate]

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.

like image 789
Kanak Vaghela Avatar asked Apr 13 '11 06:04

Kanak Vaghela


People also ask

Can you add dates in Javascript?

To add days to a date in JavaScript, you can use the setDate() and getDate() methods of the Date object. These methods are used to set and get the day of the month of the Date object.


1 Answers

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); 
like image 101
Daniel Protopopov Avatar answered Oct 07 '22 22:10

Daniel Protopopov