试一试
const event = new Date("December 31, 1975 23:15:30 GMT-3:00");
console.log(event.toUTCString());
// Expected output: "Thu, 01 Jan 1976 02:15:30 GMT"
console.log(event.getUTCMonth());
// Expected output: 0
event.setUTCMonth(11);
console.log(event.toUTCString());
// Expected output: "Wed, 01 Dec 1976 02:15:30 GMT"
语法
js
setUTCMonth(monthValue)
setUTCMonth(monthValue, dateValue)
参数
monthValue-
一个整数,表示月份:0 表示一月,1 表示二月,以此类推。
dateValue可选-
一个从 1 到 31 的整数,表示月份中的日期。
返回值
该方法会直接修改 Date 对象,并返回其新的 时间戳。如果某个参数是 NaN(或被 强制转换 为 NaN 的值,如 undefined),则日期将设置为 无效日期,并返回 NaN。
描述
如果您不指定 dateValue 参数,则会使用 getUTCDate() 方法返回的值。
如果您指定的参数超出了预期范围,setUTCMonth() 会尝试相应地更新 Date 对象中的日期信息。例如,如果您为 monthValue 使用 15,则年份将增加 1,月份将使用 3。
示例
使用 setUTCMonth()
js
const theBigDay = new Date();
theBigDay.setUTCMonth(11);
规范
| 规范 |
|---|
| ECMAScript® 2026 语言规范 # sec-date.prototype.setutcmonth |
浏览器兼容性
加载中…