I have a page which allows a user to view and edit times (with dates) stored in a database. The times are stored in the database in UTC and attached to a location with a different timezone. When the user views the times they are shown in the attached timezone and not necessarily the timezone of the browser. The user should then be able to edit a time and make and AJAX request. I need to be able to convert the inputted time from an arbitrary timezone to UTC in javascript, but the only conversion javascript seems to allow is from browser timezone to UTC. Passing an absolute offset in javascript is not going to work because the user should be able to edit times before or after daylight savings. This seems like it should be a problem that has been solved before... any pointers?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- Carriage Return (ASCII chr 13) is missing from tex
- void before promise syntax
- Keeping track of variable instances
I don't think you have to do the timezone conversion on the browser. You can just display an "opaque" date time on the client, let them modify it, then post to the server to convert it to UTC, as required.
Not sure if I should put this as an answer but I cannot leave a comment as I did not associate that temporary account with this ID...
Unfortunately it looks like doing the time zone calculation server side is the only clean way to do this, however I'm still hopeful someone might have another solution. By convention in this code all times should be sent in UTC and I'd like to avoid breaking that convention (and making it confusing in other places we might use the service call) if possible.
This is an extremely nasty problem. As long as the user is presented with all times in his/her own timezone (or in GMT), you would have no problem. But asking JS to provide accurate local Daylight Saving Time values for different locales is nasty stuff. The browser is aware of GMT and local time and (thanks to the OS) it also knows when DST kicks in locally. But without changing the user's system timezone information, JS has no way of telling when DST kicks in in another locale. The rules of when DST comes into effect not only differ from country to country, the date can change unexpectedly over time (remember when North American countries recently moved DST dates?). You can likely figure out the locale DST details on your server pretty easily and communicate that to the browser. But it's probably not practical to attempt it entirely in the browser. You could have a sort of DST_changeover_by_locale array in your application, but you'd need to update it periodically.
[EDIT] There seems to be some confusion about how the JS Date object works in regards to timezones. Here are some useful points:
Timezone offsets
Parsing dates to local time
Timestamps