jQuery 获取时区名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18246547/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 21:15:55  来源:igfitidea点击:

Get name of time zone

jquerytimezone

提问by Nathan Nguy?n

Can I get the name of the time zone of the client by using jQuery?

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

Example: Pacific S.A. Standard Time, Greenwich Standard Time, like this: https://en.wikipedia.org/wiki/List_of_time_zones_by_country

示例:南太平洋标准时间、格林威治标准时间,如下所示:https: //en.wikipedia.org/wiki/List_of_time_zones_by_country

Timezonelist

时区列表

回答by Matt Johnson-Pint

Can I get the name of the time zone of the client by using jQuery?

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

No. jQuery has only one date/time function, which is $.now().

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

If you meant JavaScript, the answer is still no. You can only obtain a time zone offsetfrom date.getTimezoneOffset(). You can't get a time zone - at least not in all browsers. Refer to the timezone tag wiki's section titled: "Time Zone != Offset"

如果您指的是JavaScript,答案仍然是否定的。您只能获得一个时区偏移距离date.getTimezoneOffset()。您无法获得时区 - 至少不是在所有浏览器中。请参阅时区标签 wiki的标题为:"Time Zone != Offset"

You can guessat the time zone, by using the jsTimeZoneDetectlibrary, but it is just a guess. It may or may not be accurate.

您可以通过使用jsTimeZoneDetect库来猜测时区,但这只是一种猜测。它可能准确,也可能不准确。

You can also now use moment.jswith the moment-timezoneadd on. It now supports time zone guessing with moment.tz.guess().

您现在还可以使用带有moment-timezone插件的moment.js。它现在支持时区猜测。moment.tz.guess()

If you can guarantee your users are running in an environment that fully supports the ECMAScript Internationalization API, you can get the user's time zone like this:

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

Intl.DateTimeFormat().resolvedOptions().timeZone

You can review the compatibility table, under DateTimeFormat- resolvedOptions().timezone defaults to the host environmentto determine which environments this will work in.

您可以查看兼容性列表,下DateTimeFormat-resolvedOptions().timezone defaults to the host environment确定这将工作中的环境。

Honestly, the best thing to do is to just give your user a screen somewhere that they can select their timezone. You might use a drop-down list, or you might use a map-based timezone picker - like this one. You can use jsTimeZoneDetect as a default value, but your user should be able to change it.

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

Also, all of these are going to give you an IANA time zone identifier, such as America/Los_Angeles. But the examples you gave appear to be Windows time zone ids (for use with TimeZoneInfoin .net). You should read the timezone tag wiki, and then also this question: How to translate between Windows and IANA time zones?

此外,所有这些都会为您提供 IANA 时区标识符,例如America/Los_Angeles. 但是您提供的示例似乎是 Windows 时区 ID(用于TimeZoneInfo在 .net 中使用)。您应该阅读时区标签 wiki,然后还有这个问题:如何在 Windows 和 IANA 时区之间进行转换?

回答by Juan Mendes

The output of new Date()is Wed Aug 14 2013 22:02:13 GMT-0700 (Pacific Daylight Time), can you use that? If you don't need the official name, you can just use what's in between the parentheses

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

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

getTimeZone(); // Pacific Daylight Time 

回答by Andriy Ivaneyko

As an alternative below is the way to get timezone with pure javascript:

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

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

回答by user3119287

Get the current timezone standard name:

获取当前时区标准名称:

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;
}

回答by Josyan

Found this script I am using with moment.js:

找到了我在 moment.js 中使用的这个脚本:

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

http://pelepim.bitbucket.org/jstz/

This script allows to get the name of the timezone of the browser, like in this exemple:

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

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

It is not in jQuery, however...

但是,它不在 jQuery 中...

回答by Rakesh Chaudhari

In pure javascript using regular expression,

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

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

回答by Kareem

Can't think of any way to do it using Jquery. You can get the remote user country name using PHP and then compare the output to:

想不出任何使用 Jquery 的方法。您可以使用 PHP 获取远程用户国家/地区名称,然后将输出与:

1) an array of countries that apply light saving time (not many).

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

2) current date.

2) 当前日期。

To get the users country:

要获取用户国家/地区:

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]
?>

This world Time Zones map link can be used to create the array: http://www.worldtimezone.com/daylight.html

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

Hope this idea works for you!

希望这个想法对你有用!

回答by Digital Alchemist

These Links might help you out timeZone using Jquery

这些链接可能会帮助您使用 Jquery解决timeZone

or following code might help u out

或以下代码可能会帮助你

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

checkout JSFIddleimplementation or for further explanation please visit Solutionby Scott

结帐的jsfiddle实现或作进一步的解释,请访问解决方案斯科特

For Only Time Zone

仅限时区

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

}

The alert popup will display Timezone as well, which you can re-factor as per your requirement, check it out in JS Fiddle

警报弹出窗口也将显示时区,您可以根据您的要求对其进行重构,在 JS Fiddle 中查看

回答by ncubica

using moment.js + moment-timezones.js

使用 moment.js + moment-timezones.js

moment.tz.guess();

moment.tz.guess();

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

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

回答by Vivek Amirthakadeswaran

This site has some nice examples

这个网站有一些很好的例子

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

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)