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 |
浏览器兼容性
加载中…