Date.prototype.getMonth()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
getMonth()
方法根据本地时间,返回一个指定的日期对象的月份,为基于 0 的值(0 表示一年中的第一月)。
尝试一下
const moonLanding = new Date("July 20, 69 00:20:18");
console.log(moonLanding.getMonth()); // (January gives 0)
// Expected output: 6
语法
js
getMonth()
参数
无
返回值
getMonth
返回一个 0 到 11 的整数值:0 代表一月份,1 代表二月份,2 代表三月份,依次类推。
示例
使用 getMonth()
下面第二条语句,基于 Date
对象 Xmas95 的值,把 11 赋值给变量 month
。
js
var Xmas95 = new Date("December 25, 1995 23:15:30");
var month = Xmas95.getMonth();
console.log(month); // 11
规范
Specification |
---|
ECMAScript® 2025 Language Specification # sec-date.prototype.getmonth |
浏览器兼容性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
getMonth |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.