使用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。