获取时区名称

时间:2013-08-15 04:54:56

标签: jquery timezone

我可以使用jQuery获取客户端的时区名称吗?

示例:Pacific S.A.标准时间,格林威治标准时间,如下所示:https://en.wikipedia.org/wiki/List_of_time_zones_by_country

Timezonelist

13 个答案:

答案 0 :(得分:43)

  

我可以使用jQuery获取客户端的时区名称吗?

没有。 jQuery只有一个日期/时间函数,即$.now()

如果您的意思是 JavaScript ,答案仍然是。您只能从date.getTimezoneOffset()获取偏移量的时区。你无法获得时区 - 至少在所有浏览器中都没有。请参阅the timezone tag wiki标题为"Time Zone != Offset"

的部分

您可以使用jsTimeZoneDetect库在时区猜测,但这只是猜测。它可能准确也可能不准确。

现在,您也可以使用moment.js添加moment-timezone。它现在支持使用moment.tz.guess()进行时区猜测。

如果您可以保证您的用户在完全支持ECMAScript Internationalization API的环境中运行,您可以像这样获取用户的时区:

Intl.DateTimeFormat().resolvedOptions().timeZone

您可以查看DateTimeFormat - resolvedOptions().timezone defaults to the host environment下的compatibility table,以确定适用的环境。

老实说,最好的办法就是给用户一个可以选择时区的屏幕。您可以使用下拉列表,也可以使用基于地图的时区选择器 - like this one。您可以使用jsTimeZoneDetect作为默认值,但您的用户应该能够更改它。

此外,所有这些都将为您提供IANA时区标识符,例如America/Los_Angeles。但是您提供的示例似乎是Windows时区ID(用于.net中的TimeZoneInfo)。您应该阅读the timezone tag wiki,然后再阅读这个问题:How to translate between Windows and IANA time zones?

答案 1 :(得分:14)

new Date()的输出为Wed Aug 14 2013 22:02:13 GMT-0700 (Pacific Daylight Time),您可以使用它吗?如果您不需要正式名称,可以使用括号中的内容

function getTimeZone() {
    return /\((.*)\)/.exec(new Date().toString())[1];
}

getTimeZone(); // Pacific Daylight Time 

答案 2 :(得分:13)

下面的替代方法是使用纯javascript 获取时区的方法:

(new Date).toString().split('(')[1].slice(0, -1)

答案 3 :(得分:7)

获取当前时区标准名称:

function getTimezoneName() {
    var timeSummer = new Date(Date.UTC(2005, 6, 30, 0, 0, 0, 0));
    var summerOffset = -1 * timeSummer.getTimezoneOffset();
    var timeWinter = new Date(Date.UTC(2005, 12, 30, 0, 0, 0, 0));
    var winterOffset = -1 * timeWinter.getTimezoneOffset();
    var timeZoneHiddenField;

    if (-720 == summerOffset && -720 == winterOffset) { timeZoneHiddenField = 'Dateline Standard Time'; }
    else if (-660 == summerOffset && -660 == winterOffset) { timeZoneHiddenField = 'UTC-11'; }
    else if (-660 == summerOffset && -660 == winterOffset) { timeZoneHiddenField = 'Samoa Standard Time'; }
    else if (-660 == summerOffset && -600 == winterOffset) { timeZoneHiddenField = 'Hawaiian Standard Time'; }
    else if (-570 == summerOffset && -570 == winterOffset) { timeZoneHiddenField.value = 'Pacific/Marquesas'; }
    //                else if (-540 == summerOffset && -600 == winterOffset) { timeZoneHiddenField.value = 'America/Adak'; }
    //                else if (-540 == summerOffset && -540 == winterOffset) { timeZoneHiddenField.value = 'Pacific/Gambier'; }
    else if (-480 == summerOffset && -540 == winterOffset) { timeZoneHiddenField = 'Alaskan Standard Time'; }
    //                else if (-480 == summerOffset && -480 == winterOffset) { timeZoneHiddenField = 'Pacific/Pitcairn'; }
    else if (-420 == summerOffset && -480 == winterOffset) { timeZoneHiddenField = 'Pacific Standard Time'; }
    else if (-420 == summerOffset && -420 == winterOffset) { timeZoneHiddenField = 'US Mountain Standard Time'; }
    else if (-360 == summerOffset && -420 == winterOffset) { timeZoneHiddenField = 'Mountain Standard Time'; }
    else if (-360 == summerOffset && -360 == winterOffset) { timeZoneHiddenField = 'Central America Standard Time'; }
    //                else if (-360 == summerOffset && -300 == winterOffset) { timeZoneHiddenField = 'Pacific/Easter'; }
    else if (-300 == summerOffset && -360 == winterOffset) { timeZoneHiddenField = 'Central Standard Time'; }
    else if (-300 == summerOffset && -300 == winterOffset) { timeZoneHiddenField = 'SA Pacific Standard Time'; }
    else if (-240 == summerOffset && -300 == winterOffset) { timeZoneHiddenField = 'Eastern Standard Time'; }
    else if (-270 == summerOffset && -270 == winterOffset) { timeZoneHiddenField = 'Venezuela Standard Time'; }
    else if (-240 == summerOffset && -240 == winterOffset) { timeZoneHiddenField = 'SA Western Standard Time'; }
    else if (-240 == summerOffset && -180 == winterOffset) { timeZoneHiddenField = 'Central Brazilian Standard Time'; }
    else if (-180 == summerOffset && -240 == winterOffset) { timeZoneHiddenField = 'Atlantic Standard Time'; }
    else if (-180 == summerOffset && -180 == winterOffset) { timeZoneHiddenField = 'Montevideo Standard Time'; }
    else if (-180 == summerOffset && -120 == winterOffset) { timeZoneHiddenField = 'E. South America Standard Time'; }
    else if (-150 == summerOffset && -210 == winterOffset) { timeZoneHiddenField = 'Mid-Atlantic Standard Time'; }
    else if (-120 == summerOffset && -180 == winterOffset) { timeZoneHiddenField = 'America/Godthab'; }
    else if (-120 == summerOffset && -120 == winterOffset) { timeZoneHiddenField = 'SA Eastern Standard Time'; }
    else if (-60 == summerOffset && -60 == winterOffset) { timeZoneHiddenField = 'Cape Verde Standard Time'; }
    else if (0 == summerOffset && -60 == winterOffset) { timeZoneHiddenField = 'Azores Daylight Time'; }
    else if (0 == summerOffset && 0 == winterOffset) { timeZoneHiddenField = 'Morocco Standard Time'; }
    else if (60 == summerOffset && 0 == winterOffset) { timeZoneHiddenField = 'GMT Standard Time'; }
    else if (60 == summerOffset && 60 == winterOffset) { timeZoneHiddenField = 'Africa/Algiers'; }
    else if (60 == summerOffset && 120 == winterOffset) { timeZoneHiddenField = 'Namibia Standard Time'; }
    else if (120 == summerOffset && 60 == winterOffset) { timeZoneHiddenField = 'Central European Standard Time'; }
    else if (120 == summerOffset && 120 == winterOffset) { timeZoneHiddenField = 'South Africa Standard Time'; }
    else if (180 == summerOffset && 120 == winterOffset) { timeZoneHiddenField = 'GTB Standard Time'; }
    else if (180 == summerOffset && 180 == winterOffset) { timeZoneHiddenField = 'E. Africa Standard Time'; }
    else if (240 == summerOffset && 180 == winterOffset) { timeZoneHiddenField = 'Russian Standard Time'; }
    else if (240 == summerOffset && 240 == winterOffset) { timeZoneHiddenField = 'Arabian Standard Time'; }
    else if (270 == summerOffset && 210 == winterOffset) { timeZoneHiddenField = 'Iran Standard Time'; }
    else if (270 == summerOffset && 270 == winterOffset) { timeZoneHiddenField = 'Afghanistan Standard Time'; }
    else if (300 == summerOffset && 240 == winterOffset) { timeZoneHiddenField = 'Pakistan Standard Time'; }
    else if (300 == summerOffset && 300 == winterOffset) { timeZoneHiddenField = 'West Asia Standard Time'; }
    else if (330 == summerOffset && 330 == winterOffset) { timeZoneHiddenField = 'India Standard Time'; }
    else if (345 == summerOffset && 345 == winterOffset) { timeZoneHiddenField = 'Nepal Standard Time'; }
    else if (360 == summerOffset && 300 == winterOffset) { timeZoneHiddenField = 'N. Central Asia Standard Time'; }
    else if (360 == summerOffset && 360 == winterOffset) { timeZoneHiddenField = 'Central Asia Standard Time'; }
    else if (390 == summerOffset && 390 == winterOffset) { timeZoneHiddenField = 'Myanmar Standard Time'; }
    else if (420 == summerOffset && 360 == winterOffset) { timeZoneHiddenField = 'North Asia Standard Time'; }
    else if (420 == summerOffset && 420 == winterOffset) { timeZoneHiddenField = 'SE Asia Standard Time'; }
    else if (480 == summerOffset && 420 == winterOffset) { timeZoneHiddenField = 'North Asia East Standard Time'; }
    else if (480 == summerOffset && 480 == winterOffset) { timeZoneHiddenField = 'China Standard Time'; }
    else if (540 == summerOffset && 480 == winterOffset) { timeZoneHiddenField = 'Yakutsk Standard Time'; }
    else if (540 == summerOffset && 540 == winterOffset) { timeZoneHiddenField = 'Tokyo Standard Time'; }
    else if (570 == summerOffset && 570 == winterOffset) { timeZoneHiddenField = 'Cen. Australia Standard Time'; }
    else if (570 == summerOffset && 630 == winterOffset) { timeZoneHiddenField = 'Australia/Adelaide'; }
    else if (600 == summerOffset && 540 == winterOffset) { timeZoneHiddenField = 'Asia/Yakutsk'; }
    else if (600 == summerOffset && 600 == winterOffset) { timeZoneHiddenField = 'E. Australia Standard Time'; }
    else if (600 == summerOffset && 660 == winterOffset) { timeZoneHiddenField = 'AUS Eastern Standard Time'; }
    else if (630 == summerOffset && 660 == winterOffset) { timeZoneHiddenField = 'Australia/Lord_Howe'; }
    else if (660 == summerOffset && 600 == winterOffset) { timeZoneHiddenField = 'Tasmania Standard Time'; }
    else if (660 == summerOffset && 660 == winterOffset) { timeZoneHiddenField = 'West Pacific Standard Time'; }
    else if (690 == summerOffset && 690 == winterOffset) { timeZoneHiddenField = 'Central Pacific Standard Time'; }
    else if (720 == summerOffset && 660 == winterOffset) { timeZoneHiddenField = 'Magadan Standard Time'; }
    else if (720 == summerOffset && 720 == winterOffset) { timeZoneHiddenField = 'Fiji Standard Time'; }
    else if (720 == summerOffset && 780 == winterOffset) { timeZoneHiddenField = 'New Zealand Standard Time'; }
    else if (765 == summerOffset && 825 == winterOffset) { timeZoneHiddenField = 'Pacific/Chatham'; }
    else if (780 == summerOffset && 780 == winterOffset) { timeZoneHiddenField = 'Tonga Standard Time'; }
    else if (840 == summerOffset && 840 == winterOffset) { timeZoneHiddenField = 'Pacific/Kiritimati'; }
    else { timeZoneHiddenField = 'US/Pacific'; }
    return timeZoneHiddenField;
}

答案 4 :(得分:5)

找到我正在使用的脚本:moment.js:

http://pellepim.bitbucket.org/jstz/

此脚本允许获取浏览器时区的名称,如下例所示:

> var timezone = jstz.determine();
> timezone.name(); 
"America/New_York"

它不是在jQuery中,但是......

答案 5 :(得分:1)

无法想到使用Jquery做任何事情。您可以使用PHP获取远程用户国家/地区名称,然后将输出比较为:

1)应用节约时间(不多)的一系列国家。

2)当前日期。

获取用户所在国家/地区:     

function visitor_country() {
    $ip = $_SERVER["REMOTE_ADDR"];
    if(filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    if(filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
            $ip = $_SERVER['HTTP_CLIENT_IP'];
    $result = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip))
            ->geoplugin_countryName;
    return $result <> NULL ? $result : "Unknown";
}
echo visitor_country(); // Output Country name [Ex: United States]
?>

此世界时区地图链接可用于创建数组:     http://www.worldtimezone.com/daylight.html

希望这个想法适合你!

答案 6 :(得分:0)

这些链接可能会帮助您timeZone using Jquery

或以下代码可能会帮助您解决问题

var offset = (new Date()).getTimezoneOffset();
alert(offset);

结帐JSFIddle实施或进一步说明请Solution访问Scott

仅适用于时区

function get_time_zone_offset( ) {
    var current_date = new Date();
  alert(current_date);   
  //return -current_date.getTimezoneOffset() / 60;

}

警告弹出窗口也会显示时区,你可以根据你的要求重新计算,在JS Fiddle中查看

答案 7 :(得分:0)

使用moment.js + moment-timezones.js

moment.tz.guess();

https://github.com/moment/moment-timezone/issues/138

答案 8 :(得分:0)

在使用正则表达式的纯JavaScript中,

var d = new Date();
var s = d.toString();
var zoneName = s.match(".*(\\((.*)\\))")[2];

答案 9 :(得分:0)

该网站有一些不错的示例

https://www.geeksforgeeks.org/javascript-date-now/

// Use of Date.now() function 
var d = Date(Date.now()); 

// Converting the number of millisecond in date string 
a = d.toString() 

// Printing the current date                     
document.write("The current date is: " + a)  

答案 10 :(得分:0)

console.log(Intl.DateTimeFormat().resolvedOptions().timeZone);

Intl.DateTimeFormat().resolvedOptions().timeZone

答案 11 :(得分:-1)

我知道这个问题已经过时了,但我想出了一个小小的解决方案:

(new Date).toLocaleString('en-US', {
    hour12: false,
    hour: '2-digit',
    timeZoneName: 'long'
}).replace(/^\d\d /, '');

我还没有对此进行过广泛的测试,但对于我的浏览器,这给了我:

"Mountain Daylight Time"

希望这有助于那里的人。

答案 12 :(得分:-1)

没有直接的方法,但你可以这样做:

var dd = new Date(); var d = dd.toString();

var startOff = d.indexOf('(')+ 1; var endOff = d.lastIndexOf(')');

var str = d.substring(startOff,endOff);

我创建了一个JS小提琴,您可以在其中看到警报: JSFiddle

var dd = new Date();
var d = dd.toString();

alert(d);
var startOff= d.indexOf('(') + 1;
alert(startOff);
var endOff= d.lastIndexOf(')');
alert(startOff+ "  : " + endOff);

var str = d.substring(startOff , endOff);
alert(str);