js加减月份,当月份超过12个月,年份会相应变化

下面代码实际测试是没有问题的。

$("#date").click(function () {
var temp = new Date(2018,7,7);
alert(temp);
// 原来是2018年8月7日,加6个月后应该是2019年2月7日
temp.setMonth(temp.getMonth() + 1 + 6);//由于设计缺陷,要在设置月份的时候就加1
alert(temp.getFullYear());// 2019
alert(temp.getMonth());// 2
alert(temp.getDate());// 7
});
posted @ 2019-08-28 17:01  阿拉灯参丁  阅读(1738)  评论(0)    收藏  举报