JavaScript function to add X months to a date

后端 未结 19 2102
星月不相逢
星月不相逢 2020-11-22 02:44

I’m looking for the easiest, cleanest way to add X months to a JavaScript date.

I’d rather not handle the rolling over of the year or have to write my own function.<

19条回答
  •  灰色年华
    2020-11-22 03:22

    d = new Date();
    
    alert(d.getMonth()+1);
    

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

提交回复
热议问题