简体   繁体   English

如何调整 Temporal ZonedDateTime 的只读属性?

[英]How to adjust the readonly properties for Temporal ZonedDateTime?

Looking into using a polypill for javascript temporal.研究对 javascript temporal 使用 polypill。

If I have Temporal.now, how do I adjust that to the 8th hour of today for example.例如,如果我有 Temporal.now,如何将其调整为今天的第 8 小时。 Do I parse out the properties and make a new ZonedDateTime or is there a better way?我是解析属性并创建一个新的 ZonedDateTime 还是有更好的方法?

Generally, use the with() method.通常,使用with()方法。 It creates a new Temporal.ZonedDateTime which is a copy of the original, but any properties present on the method's argument override the ones already present on the original.它创建了一个新的 Temporal.ZonedDateTime,它是原始的副本,但是方法参数中存在的任何属性都会覆盖原始属性中已经存在的属性。

So, if you already have an object zdt , and you want the hour to be 8, use因此,如果您已经有一个 object zdt ,并且您希望小时为 8,请使用

zdt.with({ hour: 8 })
// e.g. 2022-03-13T08:55:59.853514471-07:00[America/Vancouver]

Although, I wasn't sure from your description if this is exactly what you want;虽然,从您的描述中我不确定这是否正是您想要的; you might get today at 08:55:59.something, as above.你今天可能会在 08:55:59.something,如上。 If you want the time to be 8 o'clock, use the withPlainTime() method, which creates a new object and replaces all of the time-related properties at once:如果您希望时间为 8 点,请使用withPlainTime()方法,该方法会创建一个新的 object 并立即替换所有与时间相关的属性:

zdt.withPlainTime({ hour: 8 })
// e.g. 2022-03-13T08:00:00-07:00[America/Vancouver]

or, if you literally want the 8th hour of today, beware that that is technically not always 08:00, for example if your time zone has a daylight saving time transition that day (as in this example, America/Vancouver did on 2022-03-13; the clock skipped forward an hour during the night).或者,如果您确实想要今天的第 8 个小时,请注意,从技术上讲,这并不总是 08:00,例如,如果您的时区当天有夏令时转换(如本例中,美国/温哥华在 2022 年- 03-13;时钟在夜间快进了一个小时)。 Although this seems unlikelier than the other two possibilities, if the 8th hour of the day is what you need, use the startOfDay() method* and add() 8 hours:尽管这似乎比其他两种可能性更不可能,但如果您需要一天中的第 8 小时,请使用startOfDay()方法* 和add() 8 小时:

zdt.startOfDay().add({ hours: 8 })
// e.g. 2022-03-13T09:00:00-07:00[America/Vancouver]

*currently described in the documentation as a read-only property, but that is a mistake *目前在文档中描述为只读属性,但这是一个错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM