Temporal.PlainDate.prototype.toZonedDateTime()

可用性有限

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

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

toZonedDateTime() 方法用於 Temporal.PlainDate 例項,它會返回一個新的 Temporal.ZonedDateTime 物件,該物件表示此日期、指定的時間和指定的時間區域,並使用相同的日曆系統。

語法

js
toZonedDateTime(timeZone)
toZonedDateTime(info)

引數

timeZone

可以是代表 timeZone 選項的字串或 Temporal.ZonedDateTime 例項。這是一個方便的過載,因此 toZonedDateTime(timeZone) 等同於 toZonedDateTime({ timeZone }),其中 timeZone 是字串或 Temporal.ZonedDateTime。當第一個引數不是物件,或者物件的 timeZone 屬性是 undefined 時(因為 ZonedDateTime 例項有一個 timeZoneId 屬性而不是 timeZone),會選擇此過載。

info

一個包含以下部分或全部屬性的物件(按檢索和驗證的順序):

plainTime 可選

可以是代表結果 ZonedDateTime 時間部分的字串、物件或 Temporal.PlainTime 例項。它使用與 Temporal.PlainTime.from() 相同的演算法轉換為 Temporal.PlainTime 物件。預設為此日曆日期在此時間區域的第一個有效時間,通常是 "00:00:00",但如果例如夏令時跳過了午夜,則可能不同。

timeZone

一個字串或一個 Temporal.ZonedDateTime 例項,表示要使用的時區。如果是 Temporal.ZonedDateTime 例項,則使用其時區。如果是一個字串,它可以是命名時區識別符號、偏移時區識別符號,或者包含時區識別符號或偏移的日期時間字串(有關更多資訊,請參閱時區和偏移)。

返回值

一個新的 Temporal.ZonedDateTime 物件,表示此日期、plainTimetimeZone 指定的日期和時間,並在此日期的日曆系統中進行解釋。

模糊性 的情況下,始終使用 compatible 行為:如果時間落入了一個間隙,我們會按間隙長度向前移動;如果時間落入了一個模糊區域,我們會選擇兩者中較早的那個。這意味著結果 ZonedDateTime 的日期或時間可能與輸入不同。

異常

TypeError

如果 timeZone 不是字串或 Temporal.ZonedDateTime 例項,則丟擲此錯誤。

RangeError

如果 timeZone 是一個無效時區識別符號的字串,則會丟擲此錯誤。

示例

使用 toZonedDateTime()

js
const summer = Temporal.PlainDate.from("2021-07-01");
// Just time zone
const summerTime = summer.toZonedDateTime("America/New_York");
console.log(summerTime.toString()); // 2021-07-01T00:00:00-04:00[America/New_York]

const winter = Temporal.PlainDate.from("2021-01-01");
// Time zone and time
const winterTime = winter.toZonedDateTime({
  plainTime: "12:34:56",
  timeZone: "America/New_York",
});
console.log(winterTime.toString()); // 2021-01-01T12:34:56-05:00[America/New_York]

const spring = Temporal.PlainDate.from("2021-03-01");
// Time zone as object and time as object
const springTime = spring.toZonedDateTime({
  plainTime: summerTime.toPlainTime(),
  timeZone: winterTime,
});
console.log(springTime.toString()); // 2021-03-01T00:00:00-05:00[America/New_York]

規範

規範
Temporal
# sec-temporal.plaindate.prototype.tozoneddatetime

瀏覽器相容性

另見