使用时区以外的其他浏览器的Javascript区时区、浏览器、Javascript

2023-09-10 17:32:44 作者:落月坠入星野

我有一个网页,其允许用户查看和存储在数据库中(与日期)编辑时间。的时间存储在以UTC数据库和连接到一个位置与一个不同的时区。当用户观看的时候,他们被显示在附着时区和浏览器的不一定是时区。然后,用户应能够编辑一个时间,使和AJAX请求。我需要能够对输入的时间从任意时区转换为UTC在JavaScript,JavaScript的但似乎允许唯一转换是从浏览器时区为UTC。传递在JavaScript中绝对偏移行不通的,因为用户应该能够前或夏时制后编辑倍。这似乎是它应该是之前已经解决的问题......任何指针?

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?

推荐答案

这是一个非常棘手的问题。只要用户psented所有的时间在他/她自己的时区(或GMT)$ P $,你也没问题。但要求JS为不同地区提供准确的本地夏令时间值是讨厌的东西。该浏览器知道GMT与本地时间和(感谢OS),也知道什么时候DST踢局部的。但在不改变用户的系统时区信息,JS没有办法告诉DST时踢在另一种语言环境。当DST生效的规则不仅来自各国不同,日期可能随着时间的推移意外更改(当年北美国家最近提出的DST日期?)。你可能可以计算出您的服务器pretty的语言环境DST细节轻松传达给浏览器。但是,它可能不切合实际的尝试完全在浏览器中。你可以有一个排序DST_changeover_by_locale阵列中的应用程序,但你需要定期更新。

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.

似乎有如何的JS日期对象的工作在问候时区一些混乱。下面是一些有用的观点:

There seems to be some confusion about how the JS Date object works in regards to timezones. Here are some useful points:

// 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

解析日期为当地时间

// The following will all return the same time string, which will be 
// the time according to the user's 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

时间戳

// 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
 
精彩推荐
图片推荐