此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

Date.prototype.setUTCMonth()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015年7月⁩.

setUTCMonth() 方法根据通用的时间来设置一个准确的月份。

尝试一下

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"

语法

dateObj.setUTCMonth(monthValue[, dayValue])

参数

monthValue

一个 0-11 的整数,代表 1 月到 12 月。

dayValue

可选参数:一个 1-31 的整数,代表一个月的天数。

返回值

这个数值是从 1970 年 1 月 1 号 00:00:00 到当前时间的毫秒数(国际通用时间)

描述

如果你没有明确书写dayValue 这个参数,那么就会从getUTCDate() 方法返回对应的数值。

如果你写了一个超过在规定的范围内的参数。setUTCMonth() 就会试图相应的更新时间信息在 Data 对象中。例如,如果你用 15 作为 monthValue 的值,那么年份就会加 1,并且月份会变成 3。(15=12+3)

示例

使用 setUTCMonth()

js
var theBigDay = new Date();
theBigDay.setUTCMonth(11);

规范

Specification
ECMAScript® 2026 Language Specification
# sec-date.prototype.setutcmonth

浏览器兼容性

更多