不正确的时区名称在javascript只在IE [英] Incorrect timezone name in javascript only in IE

查看:135
本文介绍了不正确的时区名称在javascript只在IE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面提到的代码来获取客户端时区中的时区名称/ ID。使用以下代码,我得到美国国家客户的 EDT ,但现在当我在印度时区尝试时,我将它作为 UTC 而不是 IST

I am using the below mentioned code for getting the timezone name/id in the clients timezone. Using the below code I get EDT for United state clients but now when I try it in Indian timezone I get it as UTC instead of IST.

此问题只发生在IE所有其他浏览器给我正确的值,如何我可以在IE中得到正确的价值,它是 IST

This issue is occurring only for IE all other browsers give me the correct values, how can I get the correct value in IE which is IST?

var now = new Date().toString();
var TZ = now.indexOf('(') > -1 ?
now.match(/\([^\)]+\)/)[0].match(/[A-Z]/g).join('') :
now.match(/[A-Z]{3,4}/)[0];
if (TZ == "GMT" && /(GMT\W*\d{4})/.test(now)) 
TZ = RegExp.$1;
document.write(TZ);

推荐答案

引用 MDN

toTimeString()方法特别有用,因为实现ECMA-262的兼容的
引擎在从$ b获取的字符串中可能不同$ b toString() for Date 对象,格式为
实现依赖;简单的字符串切片方法可能不是
在多个引擎之间产生一致的结果。

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.

我建议您尝试使用 .toTimeString()

(new Date()).toTimeString().match(/\(([^\)]+)\)/)[1]

我相信应该给你你想要的。

I believe that should give you what you want.

这篇关于不正确的时区名称在javascript只在IE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆