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

瀏覽器相容性

另見