如何在 JavaScript 中获取客户端时区?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10298829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get clients Time zone in JavaScript?
提问by artwl
who can write a function to get clients Time zone,return value like:EDT
EST
IST
and so on
谁可以写一个函数来获取客户端时区,返回值,如:EDT
EST
IST
等等
回答by Pranay Rana
toTimeString()
method give time with the timezone name try out below...
toTimeString()
方法给时间与时区名称尝试下面...
var d=new Date();
var n=d.toTimeString();
ouput
03:41:07 GMT+0800 (PHT) or 09:43:01 EDT
Demo
演示
or
或者
Check : Automatic Timezone Detection Using JavaScript
download jstz.min.js and add a function to your html page
下载 jstz.min.js 并向您的 html 页面添加一个函数
<script language="javascript">
function getTimezoneName() {
timezone = jstz.determine_timezone()
return timezone.name();
}
</script>
回答by vipergtsrz
Use the Date().getTimezoneOffset() function and then build a hash table from this URL timeanddateto relate it to if you want to use the time zone value.
如果要使用时区值,请使用 Date().getTimezoneOffset() 函数,然后根据此 URL timeanddate构建哈希表以将其关联。
回答by daniel
If you look at the result of calling the toString
method of a Date object, you'll get a value that's something like "Tue Apr 24 2012 23:30:54 GMT+1000 (AUS Eastern Standard Time)". This will depend on what your system locale is set to.
如果您查看调用toString
Date 对象的方法的结果,您将得到一个类似于“Tue Apr 24 2012 23:30:54 GMT+1000(澳大利亚东部标准时间)”的值。这将取决于您的系统区域设置。
From there you can match each capital letter within the parentheses.
从那里您可以匹配括号内的每个大写字母。
var paren = new Date().toString().match(/\(.+\)/);
return paren ? paren[0].match(/([A-Z])/g).join("") : "";
The catch is that not every browser will include the parenthesised values.
问题是并非每个浏览器都会包含括号中的值。
If you're targeting Firefox with a known Java plugin, you can also exploit the java
object in Javascript to create a new TimeZone (java.util.TimeZone) object based on a name (eg. "America/Los_Angeles"), then call the getDisplayName
method to give you the name.
如果您的目标是带有已知 Java 插件的 Firefox,您还可以利用java
Javascript 中的对象根据名称(例如“America/Los_Angeles”)创建一个新的 TimeZone (java.util.TimeZone) 对象,然后调用getDisplayName
方法给你的名字。
回答by btk
Use jstzto get the timezone name:
使用jstz获取时区名称:
jstz.determine().name();
It returns timezone names (like 'America/New York'
) which you can then use with moment.js
etc.
它返回时区名称(如'America/New York'
),然后您可以将其与moment.js
等一起使用。
An offset (eg from a js Date
object) is not enough, because it does not carry the additional DST information (you won't know whether the time needs to be adjusted for Daylight Savings and when).
一个偏移量(例如来自 jsDate
对象)是不够的,因为它不携带额外的 DST 信息(你不会知道是否需要调整夏令时以及何时)。
回答by Ayman Elshehawy
- get client time zone in javascript
- output from -12 : +12
you can modify the output by change on this line
-Math.round(a/60)+':'+-(a%60);
Like-Math.round(a/60);
you get //+2var a = new Date().getTimezoneOffset(); var res = -Math.round(a/60)+':'+-(a%60); res = res < 0 ?res : '+'+res; console.log(res);
- 在javascript中获取客户端时区
- -12 的输出:+12
你可以通过改变这一行来修改输出
-Math.round(a/60)+':'+-(a%60);
就像-Math.round(a/60);
你得到 //+2var a = new Date().getTimezoneOffset(); var res = -Math.round(a/60)+':'+-(a%60); res = res < 0 ?res : '+'+res; console.log(res);
回答by user8796480
This is the perfect answer to get full timezone of country-
这是获得国家/地区完整时区的完美答案-
var a = new Date($.now());
var regExp = /\(([^)]+)\)/;`enter code here`
var matches = regExp.exec(a);
alert(matches[1]);
this alert will give you output like indian standard time,american standard time,african standard time:
此警报将为您提供印度标准时间、美国标准时间、非洲标准时间等输出:
//$("#timezone option:contains(" + matches[1] + ")").attr('selected', 'selected');