Temporal.Instant.fromEpochMilliseconds()

可用性有限

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

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

Temporal.Instant.fromEpochMilliseconds() 靜態方法根據自 Unix 紀元(1970 年 1 月 1 日午夜 UTC)以來的毫秒數建立一個新的 Temporal.Instant 物件。

要將 Date 物件轉換為 Temporal.Instant 物件,請改用 Date.prototype.toTemporalInstant()

語法

js
Temporal.Instant.fromEpochMilliseconds(epochMilliseconds)

引數

epochMilliseconds

一個表示自 Unix 紀元以來的毫秒數的數字。在內部,它會被轉換為 BigInt 並乘以 1e6 以獲得納秒數。

返回值

一個表示 epochMilliseconds 指定的時間點的新的 Temporal.Instant 物件。

異常

RangeError

在以下情況之一中丟擲

  • epochMilliseconds 無法轉換為 BigInt(例如,不是整數)。
  • epochMilliseconds 不在 可表示的範圍內,該範圍是自 Unix 紀元起的 ±108 天,或約 ±273,972.6 年。

示例

使用 Temporal.Instant.fromEpochMilliseconds()

js
const instant = Temporal.Instant.fromEpochMilliseconds(0);
console.log(instant.toString()); // 1970-01-01T00:00:00Z
const vostok1Liftoff = Temporal.Instant.fromEpochMilliseconds(-275248380000);
console.log(vostok1Liftoff.toString()); // 1961-04-12T06:07:00Z
const sts1Liftoff = Temporal.Instant.fromEpochMilliseconds(355924804000);
console.log(sts1Liftoff.toString()); // 1981-04-12T12:00:04Z

規範

規範
Temporal
# sec-temporal.instant.fromepochmilliseconds

瀏覽器相容性

另見