Temporal.Instant.prototype.epochNanoseconds

可用性有限

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

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

epochNanoseconds 访问器属性属于 Temporal.Instant 实例,它返回一个 BigInt,表示自 Unix 纪元(UTC 时间 1970 年 1 月 1 日午夜)以来到此瞬间经过的纳秒数。

epochNanoseconds 的 set 访问器为 undefined。您不能直接修改此属性。要创建一个具有所需新 epochNanoseconds 值的 Temporal.Instant 对象,请改用 Temporal.Instant.fromEpochNanoseconds() 静态方法。

一个瞬间只能表示纪元周围 ±108 天(约 ±273,972.6 年)的范围,即 ±8.64e21 纳秒。尝试设置超出此边界的 epochNanoseconds 会抛出 RangeError

示例

使用 epochNanoseconds

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

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

更改 epochNanoseconds

这是允许您任意时间移动的方法

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

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

js
const instant = Temporal.Instant.from("2021-08-01T12:34:56.789Z");
const instant1hourLater = Temporal.Instant.fromEpochNanoseconds(
  instant.epochNanoseconds + 3600000000000n,
);
console.log(instant1hourLater.epochNanoseconds); // 1627824896789000000n

规范

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

浏览器兼容性

另见