Temporal.Instant.from()

可用性有限

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

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

Temporal.Instant.from() 靜態方法從另一個 Temporal.Instant 物件或 RFC 9557 字串建立新的 Temporal.Instant 物件。

語法

js
Temporal.Instant.from(info)

引數

info

以下之一:

  • 一個 Temporal.Instant 例項,用於建立該例項的副本。
  • 一個 RFC 9557 字串,其中包含日期、時間和時區偏移。時區名稱將被忽略;僅使用偏移量。

返回值

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

異常

TypeError

如果 info 不是 Temporal.Instant 例項或字串,則丟擲此異常。

RangeError

在以下情況之一中丟擲

  • 該字串不是有效的 RFC 9557 字串。
  • info 不在 可表示的範圍內,該範圍是從 Unix 紀元開始的 ±108 天,或大約 ±273,972.6 年。

示例

從字串建立 instant

js
const instant = Temporal.Instant.from("1970-01-01T00Z");
console.log(instant.toString()); // 1970-01-01T00:00:00Z

const instant2 = Temporal.Instant.from("1970-01-01T00+08:00");
console.log(instant2.toString()); // 1969-12-31T16:00:00Z

// America/New_York is UTC-5 in January 1970, not UTC+8
const instant3 = Temporal.Instant.from("1970-01-01T00+08:00[America/New_York]");
console.log(instant3.toString()); // 1969-12-31T16:00:00Z; the time zone name is ignored

從另一個 instant 建立 instant

js
const instant = Temporal.Instant.from("1970-01-01T00Z");
const instant2 = Temporal.Instant.from(instant);
console.log(instant2.toString()); // 1970-01-01T00:00:00Z

規範

規範
Temporal
# sec-temporal.instant.from

瀏覽器相容性

另見