Temporal.Instant.prototype.epochMilliseconds

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

实验性: 这是一项实验性技术
在生产中使用此技术之前,请仔细检查浏览器兼容性表格

epochMilliseconds 访问器属性是 Temporal.Instant 实例的一个属性,它返回一个整数,表示自 Unix 纪元(UTC 时间 1970 年 1 月 1 日午夜)以来到此时间点的毫秒数。它等同于将 epochNanoseconds 除以 1e6 并向下取整的结果。

epochMilliseconds 的设置访问器是 undefined。你无法直接修改此属性。要创建具有所需新 epochMilliseconds 值的 Temporal.Instant 对象,请改用 Temporal.Instant.fromEpochMilliseconds() 静态方法。

示例

使用 epochMilliseconds

js
const instant = Temporal.Instant.from("2021-08-01T12:34:56.789Z");
console.log(instant.epochMilliseconds); // 1627821296789

const instant2 = Temporal.Instant.from("1969-08-01T12:34:56.789Z");
console.log(instant2.epochMilliseconds); // -13173903211

更改 epochMilliseconds

此方法允许您移动任意时间量

js
const instant = Temporal.Instant.from("2021-08-01T12:34:56.789Z");
const instant1hourLater = instant.add({ hours: 1 });
console.log(instant1hourLater.epochMilliseconds); // 1627824896789

如果您已经知道毫秒的变化量,您也可以直接构造一个新的 Temporal.Instant 对象

js
const instant = Temporal.Instant.from("2021-08-01T12:34:56.789Z");
const instant1hourLater = Temporal.Instant.fromEpochMilliseconds(
  instant.epochMilliseconds + 3600000,
);
console.log(instant1hourLater.epochMilliseconds); // 1627824896789

规范

规范
Temporal
# sec-get-temporal.instant.prototype.epochmilliseconds

浏览器兼容性

另见