setMonth()
方法根据本地时间为一个设置年份的日期对象设置月份。
语法
dateObj.setMonth(monthValue[, dayValue])
JavaScript 1.3版本之前
dateObj.setMonth(monthValue)
参数
monthValue
- 介于 0 到 11 之间的整数(表示一月到十二月)。
dayValue
- 从 1 到 31 之间的整数,表示月份中的第几天。0为上个月最后一天
- 返回值!
- 基于 1 January 1970 00:00:00 UTC 开始计算的毫秒数
描述
如果不指定 dayValue
参数,就会使用 getDate
方法的返回值。
如果有一个指定的参数超出了合理范围,setMonth
会相应地更新日期对象中的日期信息。例如,为 monthValue
指定 15,则年份会加 1,月份将会使用 3。
例子
例子:使用setMonth方法
var theBigDay = new Date();
theBigDay.setMonth(6);
规范
规范版本 | 规范状态 | 注解 |
---|---|---|
ECMAScript 1st Edition. Implemented in JavaScript 1.0 | Standard | Initial definition. |
ECMAScript 5.1 (ECMA-262) Date.prototype.setMonth |
Standard | |
ECMAScript 2015 (6th Edition, ECMA-262) Date.prototype.setMonth |
Standard |
浏览器兼容性
BCD tables only load in the browser
The compatibility table in 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.