JavaScript function to add X months to a date

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

    Simple solution: 2678400000 is 31 day in milliseconds

    var oneMonthFromNow = new Date((+new Date) + 2678400000);
    

    Update:

    Use this data to build our own function:

    • 2678400000 - 31 day
    • 2592000000 - 30 days
    • 2505600000 - 29 days
    • 2419200000 - 28 days

提交回复
热议问题