setMonth的小问题

今天突然发现一个setMonth()的问题
我想获取上一月的时间,写了如下代码

var nowDate = new Date();
console.log(nowDate);
var lastMonth = nowDate.setMonth((nowDate.getMonth() - 1));
console.log(new Date(lastMonth).getMonth() + 1);

2018-10-31_153044.png

可以看到获取的依然是10月份。
主要是只有2018年10月31号会出现这个问题,其他时间都没有问题。

var lastMonth = nowDate.setMonth((nowDate.getMonth() - 1), 1);

但如果加上setMonth()的第二个参数,就不会出现问题。
这个问题很奇葩,希望能帮到同样出现这个问题的童鞋。

你可能感兴趣的:(setMonth的小问题)