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

View in English Always switch to English

Date.prototype.setMilliseconds()

基线 广泛可用

自 2015年7月 起,此特性已在主流浏览器中得到支持,可在大多数设备和浏览器版本中正常使用。

Date 实例的 setMilliseconds() 方法会根据本地时间设置一个日期对象的毫秒数。

尝试一下

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

console.log(event.getMilliseconds());
// Expected output: 0

event.setMilliseconds(456);

console.log(event.getMilliseconds());
// Expected output: 456

语法

js
setMilliseconds(millisecondsValue)

参数

millisecondsValue

一个在 0 到 999 之间的整数,表示毫秒数。

返回值

原地更改 Date 对象,并返回新的时间戳。如果 millisecondsValueNaN(或任何其他经强制转换后得到 NaN 的值,如 undefined),日期会被设置为无效日期,并返回 NaN

描述

如果指定的数字超出了合理范围,则 Date 对象的时间信息会被相应地更新。例如,如果指定了 1005,则秒数加 1,毫秒数为 5。

示例

使用 setMilliseconds()

js
const theBigDay = new Date();
theBigDay.setMilliseconds(100);

规范

规范
ECMAScript® 2027 Language Specification
# sec-date.prototype.setmilliseconds

浏览器兼容性

参见