Moment JS showing 1 hour of difference in some PC

2019-08-27 15:22发布

问题:

I dont know why, but in some PCs the momentjs show a wrong time. I belive this caused by daylight summer (current here in my country). But in a few PCs show correctly and anothers with 1 hour of difference (like this print screen). Apparently the configuration of all PCs that I test are equals.

Updating: In a new check, I see that firefox x chrome are showing differents times. Seems the problem is just in Chrome.

On windows registry the values HKLM\SYSTEM\CurrentControlSet\Control\TimeZoneInformation are:

HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Times Zones doesn't exists on my Registry.

The Chrome navigator.userAgent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36

The Firefox navigator.userAgent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0

.

回答1:

Thanks for providing all of the information in the question. Though, I'll say that in retrospect, this isn't really on topic for Stack Overflow, and would probably fit better on the SuperUser or ServerFault sites in the StackExchange network. Still, I think I can address your problem:

The main cause is what you wrote:

HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Times Zones doesn't exists on my Registry.

That means that somehow, for some reason, perhaps by some other piece of software or perhaps by a system administrator, that very important key has been deleted. That particular key is the root of the Windows time zone database, and contains "Dynamic DST" information - allowing the start and end of DST to vary from year to year. It is what allows a list of time zones to appear in the control panel to pick from. If indeed this key does not exist, then that list should appear empty.

Why you see Chrome an hour off, is because Chrome is internally taking the TimeZoneKeyName, and then trying to find the corresponding entry in time zone database. If it is found, then it can use the Dynamic DST from there to ensure that the JS Date object behaves correctly with regard to year-over-year changes.

The interesting part is when it is not found (because it's deleted on your machine), then Chrome doesn't apply the current year DST information that is in the keys that are present on your machine. It just assumes there is no DST. FireFox, on the other hand, appears to be falling back to use the current year DST information when the time zone database is missing.

Either way - you're dealing with undocumented behaviors because it is not a reasonable state for your computer to have that key deleted. There are lots of other things that won't necessarily work correctly (such as scheduling Outlook meetings with people in other time zones, for example).

Additionally, it appears from the screenshot of your registry, that the DaylightStart value is the one from 2017, when DST would start in October in Brazil. It now starts in November, so 00 00 0a 00 should be 00 00 0b 00 at the start of that value. This change was made in Windows with KB4093753, which doesn't appear to be present on your computer. This tells me that Windows Updates are not running on these computers. Perhaps your administrator is manually picking which updates to install, or perhaps they have not been updated at all.

With particular regard to time zones, understand that changes are unpredictable. The various governments around the world can and do change their time zone information, sometimes responsibly, sometimes haphazardly. Microsoft tracks changes to this data and produces updates for Windows accordingly. You can review these updates on the Microsoft TZ/DST blog, or in the "Updates that have been issued" section of KB914387. If you don't stay up to date, then you risk not having accurate time zone information. And if you delete the time zone registry keys as seen here, then all bets are off.

Also note that it's important to have current time zone information even for countries other than your own. Perhaps, for example, you may need to schedule a meeting with a person in another time zone. If your computer is not current with time zone data updates, then you may schedule that meeting incorrectly.

At this point, I would recommend installing the latest time zone update (regardless of country indicated) which is KB4468323 at time of writing this. Time zone updates are cumulative, so in theory installing this update should restore your missing registry key and fix the problem.

Alternatively, and probably better, just turn back on Windows Update. There are probably many, many more updates you are missing also.



回答2:

Maybe if you use moment().isDST() and act accordingly...

http://momentjs.com/docs/#/query/is-daylight-saving-time/