Using timezone other than browser zone in Javascript

I have a page that allows the user to view and edit the time (with dates) stored in the database. The time is stored in a database in UTC format and is tied to a location with a different time zone. When the user views the time that they are displayed in the attached time zone, and not necessarily the time zone of the browser. Then the user should be able to edit the time and make an AJAX request. I need to be able to convert the entered time from an arbitrary time zone in UTC to javascript, but the only javascript option that apparently allows from the browser time zone to UTC. Passing absolute offset in javascript will not work, because the user must be able to edit the time before or after daylight saving time. This seems to be a problem that has been resolved before ... any pointers?

+4
source share
3 answers

This is a very nasty problem. As long as the user is presented all the time in his time zone (or GMT), you will have no problems. But asking JS to provide the exact local daylight saving time for different places is unpleasant stuff. The browser knows the time in GMT and local time and (thanks to the OS), it also knows when the DST starts locally. But without changing the time zone information of the user system, JS cannot tell when the DST enters another locale. The rules for when a DST takes effect are not only different from country to country, the date may change over time unexpectedly (remember when the North American countries recently moved daylight saving time?). You can probably easily determine the local DST data on your server and report it to the browser. But it is probably impractical to try to fully use it in a browser. Your application may have some kind of DST_changeover_by_locale array, but you need to update it periodically.

[EDIT] There seems to be some confusion about how the JS Date object works in relation to time zones. Here are some useful points:

Time Zone Offsets

// Create a time in winter (Standard time) dt = new Date('Jan 1 2010 12:00:00 GMT-0000'); alert(dt.getTimezoneOffset()); // In Japan right now, users would see "-540" // In New York right now, users would see "300" // Create a time in summer (DST) dt = new Date('Jul 1 2010 12:00:00 GMT-0000'); alert(dt.getTimezoneOffset()); // In Japan right now, users would see "-540" // In New York right now, users would see "240" // NOTE: Japan has no DST while NY does 

Local time synchronization

 // The following will all return the same time string, which will be // the time according to the user OS time settings and locale alert((new Date('Jun 25 2010 13:30:00 GMT-0230'))); // Newfoundland time alert((new Date('Jun 25 2010 13:00:00 GMT-0300'))); // Atlantic time alert((new Date('Jun 25 2010 12:00:00 GMT-0400'))); // Eastern time alert((new Date('Jun 25 2010 11:00:00 GMT-0500'))); // Central time alert((new Date('Jun 25 2010 10:00:00 GMT-0600'))); // Mountain time alert((new Date('Jun 25 2010 9:00:00 GMT-0700'))); // Pacific time 

Timestamps

 // If you don't want to work with strings, use numbers instead. // These return the JS timestamp for the same values above. // The value is a *delta*, so it is unaffected by DST. alert((new Date('Jun 25 2010 13:30:00 GMT-0230')).getTime()); // Newfoundland time alert((new Date('Jun 25 2010 13:00:00 GMT-0300')).getTime()); // Atlantic time alert((new Date('Jun 25 2010 12:00:00 GMT-0400')).getTime()); // Eastern time alert((new Date('Jun 25 2010 11:00:00 GMT-0500')).getTime()); // Central time alert((new Date('Jun 25 2010 10:00:00 GMT-0600')).getTime()); // Mountain time alert((new Date('Jun 25 2010 9:00:00 GMT-0700')).getTime()); // Pacific time 
+3
source

I do not think you need to make a change in the time zone in the browser. You can simply display the “opaque” time on the client, let them change it, and then send it to the server to convert it to UTC, as needed.

0
source

Not sure if I should put this as an answer, but I cannot leave a comment since I did not associate this temporary account with this identifier ...

Unfortunately, it seems that processing the time zone server is the only way to do this, however I still hope that someone might have a different solution. By convention, this code should be sent to UTC all the time, and I would like to avoid violating this convention (and, if possible, confuse it in other places, we could use a service call).

0
source

Source: https://habr.com/ru/post/1313706/


All Articles