今天突然發(fā)現(xiàn)一個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號會出現(xiàn)這個問題,其他時間都沒有問題。
var lastMonth = nowDate.setMonth((nowDate.getMonth() - 1), 1);
但如果加上setMonth()的第二個參數(shù),就不會出現(xiàn)問題。
這個問題很奇葩,希望能幫到同樣出現(xiàn)這個問題的童鞋。