Temporal.PlainDate.prototype.toZonedDateTime()
toZonedDateTime() 方法用於 Temporal.PlainDate 例項,它會返回一個新的 Temporal.ZonedDateTime 物件,該物件表示此日期、指定的時間和指定的時間區域,並使用相同的日曆系統。
語法
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 物件,表示此日期、plainTime 和 timeZone 指定的日期和時間,並在此日期的日曆系統中進行解釋。
在 模糊性 的情況下,始終使用 compatible 行為:如果時間落入了一個間隙,我們會按間隙長度向前移動;如果時間落入了一個模糊區域,我們會選擇兩者中較早的那個。這意味著結果 ZonedDateTime 的日期或時間可能與輸入不同。
異常
TypeError-
如果
timeZone不是字串或Temporal.ZonedDateTime例項,則丟擲此錯誤。 RangeError-
如果
timeZone是一個無效時區識別符號的字串,則會丟擲此錯誤。
示例
使用 toZonedDateTime()
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 |
瀏覽器相容性
載入中…