Temporal.Instant.prototype.toZonedDateTimeISO()

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

实验性: 这是一项实验性技术
在生产中使用此技术之前,请仔细检查浏览器兼容性表格

toZonedDateTimeISO() 方法属于 Temporal.Instant 实例,它会返回一个新的 Temporal.ZonedDateTime 对象,该对象使用 ISO 8601 日历系统,在指定的时区表示此时间点。

语法

js
toZonedDateTimeISO(timeZone)

参数

timeZone

一个字符串或一个 Temporal.ZonedDateTime 实例,表示要使用的时区。如果是 Temporal.ZonedDateTime 实例,则使用其时区。如果是一个字符串,它可以是命名时区标识符、偏移时区标识符,或者包含时区标识符或偏移的日期时间字符串(有关更多信息,请参阅时区和偏移)。

返回值

一个新的 Temporal.ZonedDateTime 对象,它使用 ISO 8601 日历系统,在指定的时区表示此时间点。

异常

RangeError

如果时区名称无效,则会抛出错误。

TypeError

如果 timeZone 不是字符串或 Temporal.ZonedDateTime 实例,则抛出此错误。

示例

使用 toZonedDateTimeISO()

js
const instant = Temporal.Instant.from("2021-08-01T12:34:56.123456789Z");
const zonedDateTime = instant.toZonedDateTimeISO("America/New_York");
console.log(zonedDateTime.toString()); // 2021-08-01T08:34:56.123456789-04:00[America/New_York]

const localDateTime = instant.toZonedDateTimeISO(Temporal.Now.timeZoneId());
console.log(localDateTime.toString()); // This instant in your timezone

规范

规范
Temporal
# sec-temporal.instant.prototype.tozoneddatetimeiso

浏览器兼容性

另见