JavaScript get current month and display
To obtain and display the current month, you can use the following JavaScript code:
// 获取当前日期对象
var currentDate = new Date();
// 获取当前月份(返回值为0-11)
var currentMonth = currentDate.getMonth();
// 转换月份为1-12
currentMonth = currentMonth + 1;
// 显示当前月份
console.log("当前月份:" + currentMonth);
This code snippet retrieves the current date object, uses the getMonth() method to get the current month (returns a value from 0 to 11), converts the month to a 1-12 format, and finally displays the current month using the console.log() function. You can also display the month at a specific location on a webpage, by using the document.getElementById() method to retrieve the HTML element where the month is to be displayed, and then assigning the month value to the innerHTML property of that element.