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
});


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM