Temporal.ZonedDateTime.prototype.epochNanoseconds

可用性有限

此特性不是基線特性,因為它在一些最廣泛使用的瀏覽器中不起作用。

實驗性: 這是一項實驗性技術
在生產中使用此技術之前,請仔細檢查瀏覽器相容性表格

Temporal.ZonedDateTime 例項的 epochNanoseconds 訪問器屬性返回一個 BigInt,表示從 Unix 紀元(世界協調時間 1970 年 1 月 1 日午夜)到當前時刻所經過的納秒數。

epochNanoseconds 的設定訪問器是 undefined。你不能直接更改此屬性。要建立一個具有所需新 epochNanoseconds 值的新 Temporal.ZonedDateTime 物件,請改用 new Temporal.ZonedDateTime() 建構函式。

一個瞬間只能表示紀元前後 ±108 天(大約 ±273,972.6 年),即 ±8.64e21 納秒。嘗試將 epochNanoseconds 設定超出此範圍會丟擲 RangeError

示例

使用 epochNanoseconds

js
const zdt = Temporal.ZonedDateTime.from("2021-08-01T12:34:56.789Z[UTC]");
console.log(zdt.epochNanoseconds); // 1627821296789000000n

const zdt2 = Temporal.ZonedDateTime.from("1969-08-01T12:34:56.789Z[UTC]");
console.log(zdt2.epochNanoseconds); // -13173903211000000n

從 epochNanoseconds 值建立 ZonedDateTime 物件

你可以使用 Temporal.ZonedDateTime() 建構函式從 epochNanoseconds 值建立一個 Temporal.ZonedDateTime 物件。

js
const epochNanoseconds = 1627821296789000000n;
const zdt = new Temporal.ZonedDateTime(epochNanoseconds, "UTC");
console.log(zdt.toString()); // 2021-08-01T12:34:56.789+00:00[UTC]

規範

規範
Temporal
# sec-get-temporal.zoneddatetime.prototype.epochnanoseconds

瀏覽器相容性

另見