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

浏览器兼容性

另见