如何收集访问者的时区信息?
我两者都需要:
时区(例如,欧洲/伦敦) 与UTC或GMT的偏移(例如,UTC+01)
如何收集访问者的时区信息?
我两者都需要:
时区(例如,欧洲/伦敦) 与UTC或GMT的偏移(例如,UTC+01)
当前回答
function getLocalTimeZone() {
var dd = new Date();
var ddStr = dd.toString();
var ddArr = ddStr.split(' ');
var tmznSTr = ddArr[5];
tmznSTr = tmznSTr.substring(3, tmznSTr.length);
return tmznSTr;
}
示例:2018年6月21日星期四18:12:50 GMT+0530(印度标准时间)
O/P: +0530
其他回答
编辑3-19-2022 -警告:我不再推荐这种方法,因为它在多个浏览器和地区有问题。
我意识到这个答案有点离题,但我想我们中的许多人在寻找答案时也想格式化显示的时区,也许还想获得时区的缩写。所以它开始了…
如果你想让客户端时区格式化得很好,你可以依赖JavaScript Date。toString方法,执行以下操作:
var split = new Date().toString().split(" ");
var timeZoneFormatted = split[split.length - 2] + " " + split[split.length - 1];
这将为您提供“GMT-0400 (EST)”,例如,包括适用的时区分钟。
或者,使用regex你可以提取任何想要的部分:
“GMT-0400 (EDT)”:
new Date().toString().match(/([A-Z]+[\+-][0-9]+.*)/)[1]
对于“GMT-0400”:
new Date().toString().match(/([A-Z]+[\+-][0-9]+)/)[1]
对于“EDT”:
new Date().toString().match(/\(([A-Za-z\s].*)\)/)[1]
对于“-0400”:
new Date().toString().match(/([-\+][0-9]+)\s/)[1]
日期。toString参考:https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/toString
编辑10/6/2020 -上述解决方案可能不适用于所有浏览器和地区。如果可能的话,我建议你使用date-fns、luxon或dayjs这样的javascript库来提供时区支持。
这就是我的解决方案:
// For time zone:
const timeZone = /\((.*)\)/.exec(new Date().toString())[1];
// Offset hours:
const offsetHours = new Date().getTimezoneOffset() / 60;
console.log(`${timeZone}, ${offsetHours}hrs`);
你可以使用:
moment-timezone
<script src="moment.js"></script>
<script src="moment-timezone-with-data.js"></script>
// retrieve timezone by name (i.e. "America/Chicago")
moment.tz.guess();
浏览器时区检测是相当棘手的,因为浏览器提供的信息很少。
Moment Timezone在当年的一些时刻上使用Date.getTimezoneOffset()和Date.toString()来收集尽可能多的关于浏览器环境的信息。然后将该信息与加载的所有时区数据进行比较,并返回最接近的匹配。如果存在重叠,则返回人口最多的城市所在的时区。
console.log(moment.tz.guess()); // America/Chicago
同时给出偏移量和时区的一行程序就是在一个新的Date对象上调用toTimeString()。中数:
toTimeString()方法以美国英语的人类可读形式返回Date对象的时间部分。
问题是时区不是标准的IANA格式;它比IANA“大洲/城市”格式更方便用户使用。试试吧:
.toTimeString console.log(新日期()().slice (9)); .resolvedOptions console.log (Intl.DateTimeFormat () () .timeZone); console.log(new Date().getTimezoneOffset() / -60);
现在在加利福尼亚,toTimeString()返回太平洋夏令时,而Intl API返回美国/洛杉矶。在哥伦比亚,你可以使用哥伦比亚标准时间,而不是美国/波哥大标准时间。
请注意,这个问题的许多其他答案都试图通过调用Date.toString()来获取相同的信息。这种方法并不可靠,正如MDN解释的那样:
Date instances refer to a specific point in time. Calling toString() will return the date formatted in a human readable form in American English. [...] Sometimes it is desirable to obtain a string of the time portion; such a thing can be accomplished with the toTimeString() method. The toTimeString() method is especially useful because compliant engines implementing ECMA-262 may differ in the string obtained from toString() for Date objects, as the format is implementation-dependent; simple string slicing approaches may not produce consistent results across multiple engines.
作为new Date().getTimezoneOffset()和moment().format('zz')的替代方案,你也可以使用momentjs:
var offset = moment.parseZone(Date.now()).utcOffset() / 60 . var offset = moment.parseZone(Date.now()).utcOffset( console.log(抵消); < script src = " https://cdn.jsdelivr.net/momentjs/2.13.0/moment.min.js " > < /脚本>
Jstimezone也有很多bug,而且没有维护(https://bitbucket.org/pellepim/jstimezonedetect/issues?status=new&status=open)