Date.prototype.setTime()

Date 实例的 setTime() 方法更改了该日期的 timestamp,即自 epoch(定义为 UTC 1970 年 1 月 1 日开始的午夜)以来的毫秒数。

¥The setTime() method of Date instances changes the timestamp for this date, which is the number of milliseconds since the epoch, defined as the midnight at the beginning of January 1, 1970, UTC.

Try it

语法

¥Syntax

js
setTime(timeValue)

参数

¥Parameters

timeValue

表示新时间戳的整数 — 自 UTC 1970 年 1 月 1 日午夜以来的毫秒数。

返回值

¥Return value

就地更改 Date 对象,并返回其新的 timestamp。如果 timeValueNaN(或获取 coercedNaN 的其他值,例如 undefined),则日期设置为 失效日期 并返回 NaN

¥Changes the Date object in place, and returns its new timestamp. If timeValue is NaN (or other values that get coerced to NaN, such as undefined), the date is set to Invalid Date and NaN is returned.

示例

¥Examples

使用 setTime()

¥Using setTime()

js
const theBigDay = new Date("1999-07-01");
const sameAsBigDay = new Date();
sameAsBigDay.setTime(theBigDay.getTime());

规范

Specification
ECMAScript Language Specification
# sec-date.prototype.settime

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看