简体   繁体   English

对于大于 1 的月份值,EST 时区中的时区变化了 1 小时

[英]Timezone is getting change by 1 hour in EST timeZone for month value greater than 1

I am working on one of my client issue.我正在处理我的一个客户问题。 I have changed my system timezone to "(UTC-5:00) Eastern time (US & canada)".我已将系统时区更改为“(UTC-5:00) 东部时间(美国和加拿大)”。 I wrote below code in my project -我在我的项目中写了下面的代码 -

new Date(year,month.date)

I observed that if month value is 0 or 1,then it is working fine -我观察到如果月份值为 0 或 1,那么它工作正常 -

new Date(2021,1,21)
Sun Feb 21 2021 00:00:00 GMT-0500 (Eastern Standard Time)

but if month value is greater than 1, then timezone is getting change by 1 hour-但如果月份值大于 1,则时区会改变 1 小时-

new Date(2021,2,21)
Sun Mar 21 2021 00:00:00 GMT-0400 (Eastern Daylight Time)

which is causing problem.这引起了问题。 please someone suggest on this that why it is happening and what might be solution of this.请有人就此提出建议,说明为什么会发生这种情况以及可能的解决方案。 expected answer should be in javascript or angular. moment library also will be accepted.预期答案应为 javascript 或 angular。moment 图书馆也将被接受。 this issue is only reproducible for few timezone.此问题仅在少数时区可重现。 It is working fine for IST. IST 工作正常。

your date is ticking over the EST's daylight savings time你的日期正赶上美国东部时间的夏令时

You can get round this by specifying the timezone of your date, for example:您可以通过指定日期的时区来解决这个问题,例如:

new Date(2021, 3, 21).toLocaleString('en-GB', { timeZone: 'UTC' }));

will return the date formatted to en-GB UTC timezone string, or you can use something like the Date.UTC function to ensure all your dates are handled as a UTC timezone将返回格式化为 en-GB UTC 时区字符串的日期,或者您可以使用Date.UTC function之类的内容来确保所有日期都作为 UTC 时区处理

datetimes are a bit of a nightmare in JS - these might not fix your issue, but hopefully they'll point you in the right direction.日期时间在 JS 中有点像噩梦——这些可能无法解决您的问题,但希望它们能为您指明正确的方向。

In regards to packages, if you're doing a lot of datetime work i would suggest Moment or the smaller Luxon packages, they really take a lot of the headache out of JS dates关于包,如果你正在做大量的日期时间工作,我建议使用 Moment 或更小的 Luxon 包,它们确实可以解决 JS 日期带来的很多麻烦

EMCAScript uses the host system settings for timezone offset. EMCAScript 使用主机系统设置进行时区偏移。 The output of Date.prototype.toString is based on those settings and inferred rules for standard and daylight saving if observed. Date.prototype.toString 的output基于这些设置和推断的标准和夏令时规则(如果遵守)。

Given one timestamp says Eastern Standard Time and the other Eastern Daylight Time, it shows that the dates relate to times when the offset rules based on your system's regional settings change from standard time to daylight saving time.给定一个时间戳表示东部标准时间和另一个东部夏令时,它表明日期与基于系统区域设置的偏移规则从标准时间更改为夏令时的时间相关。

If you only want standard time, there should be an option in your regional settings to turn off observing daylight saving.如果您只想要标准时间,您的区域设置中应该有一个选项可以关闭观察夏令时。 But that will be a system wide setting and will potentially affect the displayed dates and times everywhere on your system.但这将是一个系统范围的设置,并且可能会影响系统各处显示的日期和时间。

Alternatively, you can use toLocaleString with a location that is in the US EST timezone but doesn't observe daylight saving, eg America/Panama.或者,您可以将toLocaleString与位于美国东部时间时区但不遵守夏令时的位置一起使用,例如美国/巴拿马。 However, the format is different to that of the default toString .但是,格式与默认的toString不同。 If you want the same format, you'll have to construct it yourself, see How to format a JavaScript date如果您想要相同的格式,则必须自己构建,请参阅如何格式化 JavaScript 日期

 console.log(new Date(2022,1,25).toLocaleString('en',{timeZone:'America/Panama', timeZoneName:'short'}));

Note that this has no effect on the Date instance itself, it only changes the values displayed when toLocaleString is called this way.请注意,这对 Date 实例本身没有影响,它只会更改以这种方式调用toLocaleString时显示的值。

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

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