JavaScript function to add X months to a date

后端 未结 19 2522
星月不相逢
星月不相逢 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:12

    I'm using moment.js library for date-time manipulations. Sample code to add one month:

    var startDate = new Date(...);
    var endDateMoment = moment(startDate); // moment(...) can also be used to parse dates in string format
    endDateMoment.add(1, 'months');
    

提交回复
热议问题