如何收集访问者的时区信息?
我两者都需要:
时区(例如,欧洲/伦敦) 与UTC或GMT的偏移(例如,UTC+01)
如何收集访问者的时区信息?
我两者都需要:
时区(例如,欧洲/伦敦) 与UTC或GMT的偏移(例如,UTC+01)
尝试Date对象的getTimezoneOffset():
var curdate = new Date()
var offset = curdate.getTimezoneOffset()
此方法返回时区偏移量(以分钟为单位),即GMT和本地时间之间的差值(以分钟为单位)。
用getTimezoneOffset ()
你可以像这样在几分钟内得到时区偏移量:
var offset = new Date().getTimezoneOffset(); console.log(抵消); //如果偏移量等于-60,则时区偏移量为UTC+01
时区偏移量是UTC和本地时间之间的差值,单位为分钟。注意,这意味着如果本地时区落后于UTC,则偏移量为正,如果落后于UTC,则偏移量为负。例如,如果您的时区是UTC+10(澳大利亚东部标准时间),则将返回-600。即使对于给定的区域,夏令时也可以防止此值为常量
Mozilla日期对象引用
请注意,并非所有时区都被整小时抵消:例如,纽芬兰是UTC减去3h 30m(将日光节约时间排除在等式之外)。
请注意,这只给你时区偏移(例如:UTC+01),它不给你时区(例如:欧洲/伦敦)。
它已经回答了如何以分钟为单位获得一个整数的偏移量,但如果有人想要本地格林尼治标准时间偏移量作为字符串,例如。“+ 1130”:
function pad(number, length){
var str = "" + number
while (str.length < length) {
str = '0'+str
}
return str
}
var offset = new Date().getTimezoneOffset()
offset = ((offset<0? '+':'-')+ // Note the reversed sign!
pad(parseInt(Math.abs(offset/60)), 2)+
pad(Math.abs(offset%60), 2))
编辑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库来提供时区支持。
我在我的项目中写了一个函数,它以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}`
}
这个值来自用户的机器,可以随时更改,所以我认为没关系,我只是想获得一个近似值,然后在我的服务器上将其转换为GMT。
例如,我来自台湾,它为我返回“+8”。
工作示例
JS
function timezone() {
var offset = new Date().getTimezoneOffset();
var minutes = Math.abs(offset);
var hours = Math.floor(minutes / 60);
var prefix = offset < 0 ? "+" : "-";
return prefix+hours;
}
$('#result').html(timezone());
HTML
<div id="result"></div>
结果
+8
作为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)
使用momentjs,您可以找到当前时区为
console.log(时刻).utcOffset ());//(-240, -120, - 60,0, 60,120, 240,等等) < script src = " https://cdn.jsdelivr.net/momentjs/2.13.0/moment.min.js " > < /脚本>
使用dayjs,您可以找到当前时区为
console.log(dayjs().utcOffset());(-240、-120、-60、0、60、120、240 等) <script src=“https://unpkg.com/dayjs@1.8.10/dayjs.min.js”></script>
这两个API都返回以分钟为单位的utc偏移量。
现在医生 Dayjs医生
使用偏移量来计算时区是一种错误的方法,您总是会遇到问题。时区和夏令时规则可能会在一年中发生几次变化,并且很难跟上变化。
要获得JavaScript格式的系统IANA时区,您应该使用
控制台日志(Intl DateTimeFormat resolvedOptions()()。timeZone)
截至2023年2月,全球93.75%的浏览器都能正常运行。
旧的兼容性信息
ecma-402/1.0说timeZone如果没有提供给构造函数,可能是未定义的。然而,未来的草案(3.0)通过更改系统默认时区修复了这个问题。
在此版本的ECMAScript国际化API中, 如果没有timeZone属性,则timeZone属性将保持未定义 提供在提供给Intl的options对象中。DateTimeFormat 构造函数。但是,应用程序不应该依赖于此,因为未来 版本可能返回一个String值,用于标识主机环境 改为当前时区。
在ecma-402/3.0草案中,它被改成了
在此版本的ECMAScript 2015国际化API中 如果没有,timeZone属性将是默认时区的名称 属性在options对象中提供 Intl。DateTimeFormat构造函数。上一版本的 timeZone属性在本例中未定义。
JavaScript:
var d = new Date(); var n = d.getTimezoneOffset(); Var时区= n / -60; console.log(时区);
你可以使用:
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
你只需要包含moment.js和jstz.js
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstimezonedetect/1.0.6/jstz.min.js"></script>
在那之后
<script>
$(function(){
var currentTimezone = jstz.determine();
var timezone = currentTimezone.name();
alert(timezone);
});
</script>
如果你只需要“MST”或“EST”时区缩写:
函数getTimeZone () { var now = new Date().toString(); var timeZone = now.replace(/.*[(](.*)[)].*/,'$1') 返回时区; } console.log (getTimeZone ());
时区(小时)-
var offset = new Date().getTimezoneOffset(); 如果(抵消< 0) console.log("您的时区是- GMT+" + (offset/-60)); 其他的 console.log("您的时区是- GMT-" + offset/60);
如果你想要像你在评论中提到的那样精确,那么你应该这样尝试-
var offset = new Date().getTimezoneOffset(); 如果(偏移<0) { var extraZero = “”; if(-offset%60<10) extraZero=“0”; console.log( “Your timezone is- GMT+” + Math.ceil(offset/-60)+“:”+extraZero+(-offset%60)); } 还 { var extraZero = “”; if(偏移量%60<10) extraZero=“0”; console.log( “Your timezone is- GMT-” + Math.floor(offset/60)+“:”+extraZero+(offset%60)); }
将OffSet转换为正数:
var offset = new Date().getTimezoneOffset();
console.log(offset);
this.timeOffSet = offset + (-2*offset);
console.log(this.timeOffSet);
这对我来说是很好的工作:
// Translation to offset in Unix Timestamp
let timeZoneOffset = ((new Date().getTimezoneOffset())/60)*3600;
你可以简单地试试这个。 它将返回您当前的机器时间
var _d = new Date(), T = 0, d = new Date(t*1000 + _d.getTime())
这个就行了。
var time = new Date(),
timestamp = Date(1000 + time.getTime());
console.log(timestamp);
Thu May 25 2017 21:35:14 GMT+0300 (IDT)
未定义的
看到这个结果操作符与Timezone相反,所以应用一些数学函数,然后验证num或多或少。
请参见MDN文档 var a = new Date().getTimezoneOffset(); var res = - math .round(a/60)+':'+-(a%60); Res = Res < 0 ? Res: '+'+ Res; console.log (res)
在新的Date()中,你可以获得偏移量,要获得时区名称,你可以这样做:
.replace .toString新的日期()() (/(.*\((.*)\).*)/, '$ 2》);
在日期的末尾得到between()值,即时区的名称。
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
同时给出偏移量和时区的一行程序就是在一个新的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.
这就是我的解决方案:
// For time zone:
const timeZone = /\((.*)\)/.exec(new Date().toString())[1];
// Offset hours:
const offsetHours = new Date().getTimezoneOffset() / 60;
console.log(`${timeZone}, ${offsetHours}hrs`);
试试这个:
new Date().toLocaleString("en-US",Intl.DateTimeFormat().resolvedOptions().timeZone)
这将在客户端的浏览器上查找timeZone。
一旦我有了这个“简单”的任务,我使用(new Date()). gettimezoneoffset() -这里广泛建议的方法。但事实证明,这个解决方案并不完全正确。 在我的例子中,由于一些未记录的原因,new Date()返回GMT+0200,而new Date(0)返回GMT+0300,这是正确的。从那时起我就一直用
(new Date(0)). gettimezoneoffset()来获得正确的时间差。
为什么不直接使用:
function timezoneOffset(date: Date) {
return 6000 * ((date.getUTCHours() - date.getHours()) * 60 + ((date.getUTCMinutes() - date.getMinutes())))
}
下面是通过将时区传递给函数来查找远程国家TimezoneOffset的解决方案。在这个例子中,'Asia/Calcutta'是时区
function getTimezoneOffset(timezone) {
LocalDate = new Date();
LocalDate.setMilliseconds(0);
const LocalOffset = LocalDate.getTimezoneOffset();
RemoteLocaleStr = LocalDate.toLocaleString('en-US', {timeZone: timezone});
RemoteDate = new Date(RemoteLocaleStr);
diff = (LocalDate.getTime()-RemoteDate.getTime()) / 1000 / 60 ;
RemoteOffset = LocalOffset + diff;
return RemoteOffset;
}
console.log(getTimezoneOffset('Asia/Calcutta'));
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat
Intl. datetimeformat()构造函数创建Intl. datetimeformat。启用对语言敏感的日期和时间格式化的DateTimeFormat对象。
Intl.DateTimeFormat().resolvedOptions().timeZone // Asia/Kolkata
这可能不是最优雅的解决方案,但却是最通用的。
这使用了Intl的timeZoneName属性。DateTimeFormat
function getTimeZone(zoneName = "long") { // set up formatter let formatter = new Intl.DateTimeFormat(undefined, { timeZoneName: zoneName }); // run formatter on current date return formatter.formatToParts(Date.now()) // extract the actual value from the formatter, only reliable way i can find to do this .find(formatted => formatted.type === "timeZoneName")['value']; } // console.log every type for (const zoneName of ['short', 'long', 'shortOffset', 'longOffset', 'shortGeneric', 'longGeneric']) { console.log(`${zoneName}: ${getTimeZone(zoneName)}`) } /* short: CDT long: Central Daylight Time shortOffset: GMT-5 longOffset: GMT-05:00 shortGeneric: CT longGeneric: Central Time */
这不仅得到格式化的GMT偏移时间(即GMT-5),还得到时区的口语化名称(即中央日光时间)。
这个方法唯一不做的是获取IANA时区。我推荐上面的答案。
据我所知,DateTimeFormat没有自定义格式化的方法,因此使用formattpart,这似乎是获得时区的唯一可靠方法。
值得注意的是,目前的ECMAscript规范中只有short和long被正式定义,其他4个选项只是标准提议的一部分,在撰写本文时,safari中明显没有,尽管它正在进行中
这个问题已经有30多个答案了,但我敢肯定,有些人来到这里,却真的不知道他们在寻找什么。
惊喜!你要打开一个棘手的罐子了!
我强烈推荐你阅读我最近写的一篇关于在JS中处理时区的头痛的文章!https://medium.com/@EyeDin/time-and-time-zone-headaches-in-javascript-ae4d873a665d
你到底在找什么?
根据您的实际用例,您实际上需要不同的东西。
1)一种将固定时间点(比如2021年11月13日旧金山晚上11:35:21)转换为用户的挂钟时间的方法,用户可能在世界上任何地方。
使用.toLocaleString()。
通过DateTimeFormat中列出的locale和选项两个参数,它有更多用于格式化的自定义参数。它甚至支持不同的日历、语言、年、月、日等格式(没有传递的参数将使用用户的默认设置,这可能是最理想的)。
作为开发人员,只需将该时间点值(例如,Sales活动或竞赛的开始时间)作为UNIX时间戳数(即UTC从1/1/1970开始的秒数,与时区无关,与dst0无关)传递给客户机,然后执行new Date(timestamp). tolocalestring()。例如,对于上面的时间点,值是1636875321000(毫秒),via +dateWithTimeZone("America/Los_Angeles",2021,10,13,23,35,21)使用这个答案的函数。
2)能够在3个月后的当地时间上午9点向用户发送通知/电子邮件
这是一个完全不同的要求!非常非常不同!
格林尼治时间8点还不够吗?
No!
这只是用户在某个时间点所在的时区,比如“现在,今天”。
您如何知道用户是否使用日光节约时间?(美国的亚利桑那州没有,其他很多国家、州甚至县都没有!) 您如何知道用户是在DST中还是不在DST中?旧金山是冬天(GMT-8)还是阿拉斯加是夏天(同样是GMT-8) ?看看这个巨大的列表,你会抓狂的:https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
“PDT”还不够吗?
No!
首先,PDT并不是全年都有的。现在是11月,timeanddate.com网站上写着“PDT目前没有位置。你想看PST吗?”(阅读我上面的Medium文章了解更多细节。)
除非你想要从客户端到客户端的UI字符串,这个PDT/CEST/等等。一点价值都没有!
为什么?因为墨西哥的蒂华纳和美国的旧金山在6月都在PDT,但他们有不同的DST(日光节约)日,他们切换到PST。你需要知道这一点!
美国2022年夏令时为2022年3月13日至11月6日。墨西哥2022年的夏令时时间为2022年4月3日至2022年10月30日。因此,虽然蒂华纳和旧金山在近93%的时间里共享“PDT/PST”,但在7%的时间里(例如,2022年的3月14日至4月3日,以及10月31日至11月6日),它们并不匹配。
所以,即使你知道现在是11月,也知道用户处于PDT状态,你也无法判断用户设备中的时间戳数字是2022年4月1日上午9点。
PS.如果你试图从new Date(). tostring()中获取PDT,情况会更糟。仅供参考,如果你将你的机器的语言环境更改为es-MX(西班牙语,这是13%的美国人的语言),你会得到这样的结果:
> new Date().toString()
'Sun Nov 14 2021 00:47:25 GMT-0800 (hora estándar del Pacífico)'
祝hedP好运!
那么,我该怎么办呢?
唯一正确的方法是使用IANA(阅读“标准”)时区值。这是维基百科页面的第二栏。它来自:
const timeZoneIANA = Intl.DateTimeFormat().resolvedOptions().timeZone;
它返回IANA值,即旧金山的America/Los_Angeles。如果有的话,这是您应该为用户存储在数据库中的值。您可以从时区获得关于它的各种信息。Json包,并将时间从/转换为该时区,加上日期(这很重要),就像这个答案。
那么IE11呢?
IE11不支持Intl....事情大多数填充物只是猜测,是不准确的。所以,试着放弃对IE11的支持,或者使用一个库,为一些不准确做好准备。
3)我只是想知道他们目前的时区偏移(例如-8:00)
你可能不应该这么做。我想不出你需要这个的真正原因。再说一次,知道今天的值,并不能告诉你明天的值是多少。
不管怎样,你总能得到这个值
new Date().getTimezoneOffset() / 60
在客户端。
注意:. gettimezoneoffset()不是date对象的时区偏移量,你是在调用它。请阅读文档。
准确地说,它接受传递的Date对象(该对象具有时间戳,引用历史中的单个时间点),然后告诉该时间点的UTC时间和本地时间之间的差值。
同样,这取决于日期。看到的:
console.log(new Date("6/13/2021").getTimezoneOffset() / 60); // It's 7 on a machine that runs in San Francisco
console.log(new Date("11/13/2021").getTimezoneOffset() / 60); // It's 8 on a machine that runs in in San Francisco
有图书馆吗?
下面是支持时区交换的库列表。
但是,请记住,您必须首先解决设计问题,然后再寻找代码。关于时区的事情很容易变得复杂,你必须知道“PDT”和“GMT-8”不是全年为用户存储在DB中的有信息/有价值的时区值。:)
正如其他人提到的,要获得时区:
const tz = Intl.DateTimeFormat().resolvedOptions().timeZone
之前没有提到,要从时区获取偏移量,使用区域设置“ia”(参见https://stackoverflow.com/a/64262840/1061871)
const getOffset = (tz) => Intl.DateTimeFormat("ia", {
timeZoneName: "shortOffset",
timeZone : tz
})
.formatToParts()
.find((i) => i.type === "timeZoneName").value // => "GMT+/-hh:mm"
.slice(3); //=> +/-hh:mm
console.log(tz + ' UTC' + getOffset(tz))