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

View in English Always switch to English

Date.prototype.setUTCMilliseconds()

基线 广泛可用

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

setUTCMilliseconds() 方法会根据世界时来设置指定时间的毫秒数。

尝试一下

const date1 = new Date("2018-01-24T12:38:29.069Z");

console.log(date1.getUTCMilliseconds());
// Expected output: 69

date1.setUTCMilliseconds(420);

console.log(date1.getUTCMilliseconds());
// Expected output: 420

语法

dateObj.setUTCMilliseconds(millisecondsValue)

参数

millisecondsValue

0 - 999 之间的数值,代表毫秒数。

返回值

返回更新后的时间距 1970 年 1 月 1 日 00:00:00 (UTC) 的毫秒数。

描述

如果传递的参数超出了指定的范围,setUTCMilliseconds() 方法会相应地尝试更新储存在 Date 的时间信息。例如,假设你传递参数的值是 1100,存储在 Date 的秒数会加 1,然后使用 100 来作为毫秒数。

示例

使用 setUTCMilliseconds() 方法

js
var theBigDay = new Date();
theBigDay.setUTCMilliseconds(500);

规范

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

浏览器兼容性

参见