Temporal.ZonedDateTime.prototype.getTimeZoneTransition()

可用性有限

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

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

Temporal.ZonedDateTime 例項的 getTimeZoneTransition() 方法返回一個 Temporal.ZonedDateTime 物件,該物件表示在此即時之後或之前,時區 UTC 偏移量發生變化的最接近的即時(返回的即時是變化後的第一個即時),如果沒有這樣的變化,則返回 null。這對於查詢時區的偏移量規則(例如夏令時模式)非常有用。

請注意,有關未來的即時可能不可靠,例如由於時區定義的變化。

語法

js
getTimeZoneTransition(direction)
getTimeZoneTransition(options)

引數

direction

一個字串,表示 direction 選項。這是一個方便的過載,因此 getTimeZoneTransition(direction) 等同於 getTimeZoneTransition({ direction }),其中 direction 是一個字串。

options

包含以下屬性的物件

direction

是搜尋當前即時之前還是之後。必須是 "next""previous" 之一。

返回值

一個 Temporal.ZonedDateTime 物件,其即時 t 滿足:

  • t 時的時區偏移量與 t 前一納秒的偏移量不同。
  • 如果 direction"previous",則 t < this.epochNanoseconds;如果 direction"next",則 t > this.epochNanoseconds
  • this.epochNanosecondst 之間的所有即時(不包括 this.epochNanosecondst),偏移量是恆定的。

如果沒有這樣的過渡,則返回 null

示例

查詢下一個時區過渡

js
const dt = Temporal.ZonedDateTime.from("2024-01-01T00-05:00[America/New_York]");
const transition = dt.getTimeZoneTransition("next");
console.log(transition.toString()); // "2024-03-10T03:00:00-04:00[America/New_York]"

const transition2 = transition.getTimeZoneTransition("next");
console.log(transition2.toString()); // "2024-11-03T01:00:00-05:00[America/New_York]"

const transition3 = dt.getTimeZoneTransition("previous");
console.log(transition3.toString()); // "2023-11-05T01:00:00-05:00[America/New_York]"

const dt2 = Temporal.ZonedDateTime.from("2024-01-01T00Z[UTC]");
console.log(dt2.getTimeZoneTransition("next")); // null

規範

規範
Temporal
# sec-temporal.zoneddatetime.prototype.gettimezonetransition

瀏覽器相容性

另見