JavaScript獲取當前月份並顯示


使用Date()獲取日期

getMonth()+1得到當前月份

var ndate = new Date();
var nmonth = ndate.getMonth()+1;

彈窗顯示

alert("當前月份:"+nmonth+"月");

控制台輸出

console.log("當前月份:"+nmonth+"月");

頁面顯示

document.write("當前月份:"+nmonth+"月");

 getMonth()的返回值是 0(1月) 到 11(12月)之間的一個整數,返回的是索引值,所以要加1。


免責聲明!

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



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