如何收集访问者的时区信息?
我两者都需要:
时区(例如,欧洲/伦敦) 与UTC或GMT的偏移(例如,UTC+01)
如何收集访问者的时区信息?
我两者都需要:
时区(例如,欧洲/伦敦) 与UTC或GMT的偏移(例如,UTC+01)
当前回答
同时给出偏移量和时区的一行程序就是在一个新的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.
其他回答
同时给出偏移量和时区的一行程序就是在一个新的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().toLocaleString("en-US",Intl.DateTimeFormat().resolvedOptions().timeZone)
这将在客户端的浏览器上查找timeZone。
用getTimezoneOffset ()
你可以像这样在几分钟内得到时区偏移量:
var offset = new Date().getTimezoneOffset(); console.log(抵消); //如果偏移量等于-60,则时区偏移量为UTC+01
时区偏移量是UTC和本地时间之间的差值,单位为分钟。注意,这意味着如果本地时区落后于UTC,则偏移量为正,如果落后于UTC,则偏移量为负。例如,如果您的时区是UTC+10(澳大利亚东部标准时间),则将返回-600。即使对于给定的区域,夏令时也可以防止此值为常量
Mozilla日期对象引用
请注意,并非所有时区都被整小时抵消:例如,纽芬兰是UTC减去3h 30m(将日光节约时间排除在等式之外)。
请注意,这只给你时区偏移(例如:UTC+01),它不给你时区(例如:欧洲/伦敦)。
我在我的项目中写了一个函数,它以hh:mm格式返回时区。我希望这能帮助到一些人:
function getTimeZone() {
var offset = new Date().getTimezoneOffset(), o = Math.abs(offset);
return (offset < 0 ? "+" : "-") + ("00" + Math.floor(o / 60)).slice(-2) + ":" + ("00" + (o % 60)).slice(-2);
}
// Outputs: +5:00
getTimeZone() { var offset = new Date().getTimezoneOffset(), o = Math.abs(offset); 返回(offset < 0 ?"+": "-") +("00" +数学。地板(o / 60)) .slice (2 ) + ":" + (" 00”+ (o % 60)) .slice (2); } //查看输出 document . write (getTimeZone ());
工作小提琴
带有注释的代码
/**
* Get client side timezone.
*
* @returns {(+|-)HH:mm} - Where `HH` is 2 digits hours and `mm` 2 digits minutes.
* @example
* // From Indian/Reunion with UTC+4
* // '+04:00'
* getTimeZone()
*/
const getTimeZone = () => {
const timezoneOffset = new Date().getTimezoneOffset()
const offset = Math.abs(timezoneOffset)
const offsetOperator = timezoneOffset < 0 ? '+' : '-'
const offsetHours = Math.floor(offset / 60).toString().padStart(2, '0')
const offsetMinutes = Math.floor(offset % 60).toString().padStart(2, '0')
return `${offsetOperator}${offsetHours}:${offsetMinutes}`
}
为什么不直接使用:
function timezoneOffset(date: Date) {
return 6000 * ((date.getUTCHours() - date.getHours()) * 60 + ((date.getUTCMinutes() - date.getMinutes())))
}