用于将X月添加到日期的JavaScript函数

用于将X月添加到日期的JavaScript函数

问题描述:

我正在寻找将X个月添加到JavaScript日期的最简单,最干净的方法。

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

我宁愿不是处理年度滚动或者必须编写我自己的函数

内置的内容可以做到吗?

Is there something built in that can do this?

我认为应该这样做:

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.

试一试