Date.prototype.setMinutes()

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

setMinutes() 方法用于修改 Date 实例的分(minutes),该方法以本地时间为准。

试一试

const event = new Date("August 19, 1975 23:15:30");

event.setMinutes(45);

console.log(event.getMinutes());
// Expected output: 45

console.log(event);
// Expected output: "Tue Aug 19 1975 23:45:30 GMT+0200 (CEST)"
// Note: your timezone may vary

语法

js
setMinutes(minutesValue)
setMinutes(minutesValue, secondsValue)
setMinutes(minutesValue, secondsValue, msValue)

参数

minutesValue

一个介于 0 和 59 之间的整数,表示分钟数。

secondsValue 可选

一个介于 0 和 59 之间的整数,表示秒数。如果指定了 secondsValue,则必须同时指定 minutesValue

msValue 可选

一个介于 0 和 999 之间的整数,表示毫秒数。如果指定了 msValue,则必须同时指定 minutesValuesecondsValue

返回值

该方法会直接修改 Date 对象,并返回其新的 时间戳。如果某个参数是 NaN(或被 强制转换NaN 的值,如 undefined),则日期将设置为 无效日期,并返回 NaN

描述

如果未指定 secondsValuemsValue 参数,则会使用通过 getSeconds()getMilliseconds() 返回的值。

如果指定的参数超出了预期范围,则会相应地更新其他参数以及 Date 对象中的日期信息。例如,如果为 secondsValue 指定了 100,则分钟数会增加 1(minutesValue + 1),秒数则为 40。

由于 setMinutes() 操作的是本地时间,因此跨越夏令时(DST)边界可能会导致与预期不同的时间差。例如,当设置分钟数时,如果跨越了春季前进(时钟向前跳一小时)的转换,新旧日期之间的时间戳差会比标称时间差少一个小时。反之,如果跨越了秋季回拨(时钟向后跳一小时)的转换,则会额外增加一个小时。如果您需要通过固定时间量来调整日期,请考虑使用 setUTCMinutes()setTime()

如果新的本地时间落在偏移量转换范围内,则确切时间将使用与 Temporaldisambiguation: "compatible" 选项相同的行为来确定。也就是说,如果本地时间对应两个瞬间,则选择较早的那个;如果本地时间不存在(存在间隙),则向前推进间隙持续的时间。

示例

使用 setMinutes()

js
const theBigDay = new Date();
theBigDay.setMinutes(45);

规范

规范
ECMAScript® 2026 语言规范
# sec-date.prototype.setminutes

浏览器兼容性

另见