是否有一种标准的方法可以让服务器在网页中确定用户的时区?
可能来自HTTP报头或用户代理字符串的一部分?
是否有一种标准的方法可以让服务器在网页中确定用户的时区?
可能来自HTTP报头或用户代理字符串的一部分?
当前回答
首先,了解JavaScript中的时区检测是不完善的。您可以在date对象的实例上使用getTimezoneOffset获取特定日期和时间的本地时区偏移量,但这与完整的IANA时区(如America/Los_Angeles)不完全相同。
以下是一些可行的方法:
大多数现代浏览器在实现ECMAScript国际化API时都支持IANA时区,所以你可以这样做:
const tzid = Intl.DateTimeFormat().solveOptions().timeZone; console.log(tzid);
结果是一个字符串,其中包含运行代码的计算机的IANA时区设置。
支持的环境列在Intl兼容性表中。展开DateTimeFormat部分,并查看名为resolvedOptions()的特性。timeZone默认为主机环境。
Some libraries, such as Luxon use this API to determine the time zone through functions like luxon.Settings.defaultZoneName. If you need to support an wider set of environments, such as older web browsers, you can use a library to make an educated guess at the time zone. They work by first trying the Intl API if it's available, and when it's not available, they interrogate the getTimezoneOffset function of the Date object, for several different points in time, using the results to choose an appropriate time zone from an internal data set. Both jsTimezoneDetect and moment-timezone have this functionality. // using jsTimeZoneDetect var tzid = jstz.determine().name(); // using moment-timezone var tzid = moment.tz.guess(); In both cases, the result can only be thought of as a guess. The guess may be correct in many cases, but not all of them. Additionally, these libraries have to be periodically updated to counteract the fact that many older JavaScript implementations are only aware of the current daylight saving time rule for their local time zone. More details on that here.
最终,更好的方法是向用户询问他们所在的时区。提供一个他们可以改变的设置。你可以使用上面的选项之一来选择一个默认设置,但不要让它不可能偏离你的应用程序。
还有一种完全不同的方法,即完全不依赖用户电脑的时区设置。相反,如果可以收集纬度和经度坐标,则可以使用以下方法之一将它们解析为时区。这在移动设备上效果很好。
其他回答
我所见过的最流行的(==标准?)确定时区的方法是简单地询问用户自己。如果你的网站需要订阅,这可以保存在用户的个人资料数据。对于anon用户,日期可以显示为UTC或GMT或类似的格式。
我可不想自作聪明。只是有时候有些问题在任何编程环境之外都有更好的解决方案。
下面是一个健壮的JavaScript解决方案,用于确定浏览器所在的时区。
>>> var timezone = jstz.determine();
>>> timezone.name();
"Europe/London"
https://github.com/pellepim/jstimezonedetect
JavaScript是获取客户端本地时间的最简单方法。我建议使用XMLHttpRequest发送回本地时间,如果失败,则退回到基于IP地址检测到的时区。
至于地理定位,我已经在几个项目中使用了MaxMind GeoIP,它工作得很好,尽管我不确定他们是否提供时区数据。这是一项你付费的服务,他们每月更新你的数据库。他们提供多种网络语言的包装器。
所有的魔力似乎都在
visitortime.getTimezoneOffset()
真酷,我还不知道呢。它能在ie浏览器上运行吗?从这里你应该能够使用JavaScript到Ajax,设置cookie等等。我自己可能也会吃饼干。
不过,您需要允许用户更改它。我们曾尝试使用地理定位(通过maxmind)来实现这一点,但这是错误的,不值得这么做。所以我们让用户在他们的配置文件中设置它,并向还没有设置的用户显示一个通知。
这里有一种更完整的方法。
获取用户的时区偏移量 测试一些日子的夏令时边界,以确定它们是否在使用夏令时的区域。
节选如下:
function TimezoneDetect(){
var dtDate = new Date('1/1/' + (new Date()).getUTCFullYear());
var intOffset = 10000; //set initial offset high so it is adjusted on the first attempt
var intMonth;
var intHoursUtc;
var intHours;
var intDaysMultiplyBy;
// Go through each month to find the lowest offset to account for DST
for (intMonth=0;intMonth < 12;intMonth++){
//go to the next month
dtDate.setUTCMonth(dtDate.getUTCMonth() + 1);
// To ignore daylight saving time look for the lowest offset.
// Since, during DST, the clock moves forward, it'll be a bigger number.
if (intOffset > (dtDate.getTimezoneOffset() * (-1))){
intOffset = (dtDate.getTimezoneOffset() * (-1));
}
}
return intOffset;
}
从JS获取TZ和DST(通过Way Back Machine)