Temporal.Instant.prototype.epochMilliseconds

可用性有限

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

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

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

epochMilliseconds 的設定訪問器是 undefined。你無法直接修改此屬性。要建立具有所需新 epochMilliseconds 值的 Temporal.Instant 物件,請改用 Temporal.Instant.fromEpochMilliseconds() 靜態方法。

示例

使用 epochMilliseconds

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

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

更改 epochMilliseconds

此方法允許您移動任意時間量

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

如果您已經知道毫秒的變化量,您也可以直接構造一個新的 Temporal.Instant 物件

js
const instant = Temporal.Instant.from("2021-08-01T12:34:56.789Z");
const instant1hourLater = Temporal.Instant.fromEpochMilliseconds(
  instant.epochMilliseconds + 3600000,
);
console.log(instant1hourLater.epochMilliseconds); // 1627824896789

規範

規範
Temporal
# sec-get-temporal.instant.prototype.epochmilliseconds

瀏覽器相容性

另見