JavaScript 如何从日期获取月份名称
本文的目的是使用JavaScript从特定日期获取月份名称 getMonth() 方法。该方法用于从给定的 Date 对象中提取月份(0到11)。它返回一个整数值,范围在0到11之间。零(0)表示一月,1表示二月,以此类推,直到11表示十二月。
语法:
DateObj.getMonth()
示例: 下面的代码用于显示属于特定日期的当前月份。
<body style="text-align:center;">
<h2 style="color:green">
GeeksForGeeks
</h2>
<h2>
How to get month name from
Date using Javascript?
</h2>
<button onclick="myFunction()">
Click Me!
</button>
<h3 id="demoID" style="color:green;">
</h3>
<script>
function myFunction() {
// initializing an array
const months = [
"January", "February",
"March", "April", "May",
"June", "July", "August",
"September", "October",
"November", "December"
];
const d = new Date();
document.getElementById(
"demoID").innerHTML =
"The current month is "
+ months[d.getMonth()];
}
</script>
</body>
输出:

极客教程