tags:

views:

85

answers:

3

Is there a built in equivalent to the .NET framework's AddMonth function or VB's DateAdd function?

I'm looking for the easiest, cleanest way to add X month to a Javascript date.

I'd rather not handle the rolloing over of the year as done here.

or have to write my own function as done here.

Is there something built in that is as nice as the .NET Date.AddMonths function? Or something close?

+1  A: 

I'm a pretty big fan of Datejs. You can use it to do what you want, plus a whole lot more.

BradBrening
+3  A: 

I think this should do it:

var x = 12; //or whatever offset
var CurrentDate = new Date();
CurrentDate.setMonth(CurrentDate.getMonth()+X);

I believe it should automatically handle incrementing to the appropriate year and mod-ing to the appropriate month.

try it

Velika
+1 - It does indeed handle the addition of years in the event that `setMonth()` is greater than 11 (remember month ints are zero-based).
Andy E
+1  A: 
d = new Date();

alert(d.getMonth()+1);

Months have a 0-based index, it should alert(4) which is 5 (may);

Ben