大网数据广告活动
Web 标准与前端开发

Temporal.ZonedDateTime.prototype.offset

· 52CE 我爱测

The **`offset`** accessor property of Temporal.ZonedDateTime instances returns a string representing the offset used to interpret the internal instant, in the form `±HH:mm` (or `±HH:mm:ss.sssssssss` with as much subminute precision as necessary). This offset is guaranteed to be v

The offset accessor property of Temporal.ZonedDateTime instances returns a string representing the offset used to interpret the internal instant, in the form ±HH:mm (or ±HH:mm:ss.sssssssss with as much subminute precision as necessary). This offset is guaranteed to be valid for the given instant and time zone at construction time.

The set accessor of offset is undefined. You cannot change this property directly. Use the Temporal/ZonedDateTime/with method to create a new Temporal.ZonedDateTime object with the desired new value (usually also changing the date/time), or use the Temporal/ZonedDateTime/withTimeZone method to create a new Temporal.ZonedDateTime object in another time zone.

Examples

Using offset

const dt = Temporal.ZonedDateTime.from(
  "2021-07-01T12:00:00-07:00[America/Los_Angeles]",
);
console.log(dt.offset); // "-07:00"

const dt2 = Temporal.ZonedDateTime.from("2021-07-01T12:00:00-07[-07]");
console.log(dt2.offset); // "-07:00"

const dt3 = Temporal.ZonedDateTime.from(
  "1900-01-01T00:00:00+00:09:21[Europe/Paris]",
);
console.log(dt3.offset); // "+00:09:21"

const dt4 = Temporal.ZonedDateTime.from("2021-07-01T12:00:00Z[Asia/Shanghai]");
console.log(dt4.offset); // "+08:00"

Changing offset

If the local time happens to have two valid offsets, such as within a DST transition, you can change the offset without changing anything else:

const zdt = Temporal.ZonedDateTime.from(
  "2024-11-03T01:05:00-04:00[America/New_York]",
);
const newZDT = zdt.with({ offset: "-05:00" });
console.log(newZDT.toString()); // "2024-11-03T01:05:00-05:00[America/New_York]"

Specifications

Browser compatibility

See also

  • Temporal.ZonedDateTime
  • Temporal/ZonedDateTime/with
  • Temporal/ZonedDateTime/withTimeZone
  • Temporal/ZonedDateTime/timeZoneId
  • Temporal/ZonedDateTime/offsetNanoseconds

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。