Temporal.ZonedDateTime.prototype.epochMilliseconds

可用性有限

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

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

Temporal.ZonedDateTime 例項的 epochMilliseconds 訪問器屬性返回一個整數,表示自 Unix 紀元(1970 年 1 月 1 日午夜,UTC)到此瞬間所經過的毫秒數。它等同於將 epochNanoseconds 除以 1e6 並向下取整。

epochMilliseconds 的設定器為 undefined。你無法直接更改此屬性。要建立一個具有所需新 epochMilliseconds 值的新 Temporal.ZonedDateTime 物件,請參見下文。

示例

使用 epochMilliseconds

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

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

從 epochMilliseconds 值建立 ZonedDateTime 物件

你可以透過首先使用 Temporal.Instant.fromEpochMilliseconds() 構造一個 Temporal.Instant 物件,然後使用 Temporal.Instant.prototype.toZonedDateTimeISO() 將其轉換為 Temporal.ZonedDateTime 物件來從 epochMilliseconds 值建立 Temporal.ZonedDateTime 物件。

js
const epochMilliseconds = 1627821296789;
const instant = Temporal.Instant.fromEpochMilliseconds(epochMilliseconds);
const zdt = instant.toZonedDateTimeISO("UTC");
console.log(zdt.toString()); // 2021-08-01T12:34:56.789+00:00[UTC]

或者,使用 Temporal.ZonedDateTime() 建構函式,但首先將毫秒轉換為納秒。

js
const epochMilliseconds = 1627821296789;
const epochNanoseconds = BigInt(epochMilliseconds) * 1_000_000n;
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.epochmilliseconds

瀏覽器相容性

另見