简体   繁体   English

具有时差时区的时区

[英]timezones with moment-timezone

I am checking the list of timezones using moment-timezone from moment.js 我正在使用来自moment.js的moment-timezone列表

moment.tz.names()

is giving 583 item , the list is very detailed and very huge, how can I get the main timezones out of it so I create a drop down list? 提供了583项目,该列表非常详细且非常庞大,如何从中获取主要时区,所以我创建了一个下拉列表?

it goes like this : 它是这样的:

"Africa/Abidjan", "Africa/Accra", "Africa/Addis_Ababa", "Africa/Algiers", "Africa/Asmara", "Africa/Asmera", "Africa/Bamako", "Africa/Bangui", "Africa/Banjul", "Africa/Bissau", "Africa/Blantyre", "Africa/Brazzaville", "Africa/Bujumbura", "Africa/Cairo", "Africa/Casablanca", "Africa/Ceuta", "Africa/Conakry", "Africa/Dakar", "Africa/Dar_es_Salaam", "Africa/Djibouti", "Africa/Douala", "Africa/El_Aaiun", "Africa/Freetown", "Africa/Gaborone", "Africa/Harare", "Africa/Johannesburg", "Africa/Juba", "Africa/Kampala", "Africa/Khartoum", "Africa/Kigali", "Africa/Kinshasa", "Africa/Lagos", "Africa/Libreville", "Africa/Lome", "Africa/Luanda", "Africa/Lubumbashi", "Africa/Lusaka", "Africa/Malabo", "Africa/Maputo", "Africa/Maseru", "Africa/Mbabane", "Africa/Mogadishu", "Africa/Monrovia", "Africa/Nairobi", "Africa/Ndjamena", "Africa/Niamey", "Africa/Nouakchott", "Africa/Ouagadougou", "Africa/Porto-Novo", "Africa/Sao_Tome", "Africa/Timbuktu", "Africa/Tripoli", "Africa/Tunis", "Africa/Windhoek", "Americ “非洲/阿比让”,“非洲/阿克拉”,“非洲/亚的斯亚贝巴”,“非洲/阿尔及尔”,“非洲/阿斯马拉”,“非洲/阿美拉”,“非洲/巴马科”,“非洲/班吉”,“非洲/“班珠尔”,“非洲/比绍”,“非洲/布兰太尔”,“非洲/布拉柴维尔”,“非洲/布琼布拉”,“非洲/开罗”,“非洲/卡萨布兰卡”,“非洲/休达”,“非洲/科纳克里” ”,“非洲/达喀尔”,“非洲/达累斯萨拉姆”,“非洲/吉布提”,“非洲/杜阿拉”,“非洲/ El_Aaiun”,“非洲/弗里敦”,“非洲/ Gaborone”,“非洲/哈拉雷”, “非洲/约翰内斯堡”,“非洲/朱巴”,“非洲/坎帕拉”,“非洲/喀土穆”,“非洲/基加利”,“非洲/金沙萨”,“非洲/拉各斯”,“非洲/利伯维尔”,“非洲/洛美”,“非洲/罗安达”,“非洲/卢本巴希”,“非洲/卢萨卡”,“非洲/马拉博”,“非洲/马普托”,“非洲/马塞鲁”,“非洲/马巴巴内”,“非洲/摩加迪沙” ”,“非洲/蒙罗维亚”,“非洲/内罗毕”,“非洲/恩贾梅纳”,“非洲/尼亚美”,“非洲/努瓦克肖特”,“非洲/瓦加杜古”,“非洲/ Porto-Novo”,“非洲/ Sao_Tome ”,“非洲/廷布克图”,“非洲/的黎波里”,“非洲/突尼斯”,“非洲/温得和克”,“美国 a/Adak", "America/Anchorage", "America/Anguilla", "America/Antigua", "America/Araguaina", "America/Argentina/Buenos_Aires", "America/Argentina/Catamarca", "America/Argentina/ComodRivadavia", "America/Argentina/Cordoba", "America/Argentina/Jujuy", "America/Argentina/La_Rioja", "America/Argentina/Mendoza", "America/Argentina/Rio_Gallegos", "America/Argentina/Salta", "America/Argentina/San_Juan", "America/Argentina/San_Luis", "America/Argentina/Tucuman", "America/Argentina/Ushuaia", "America/Aruba", "America/Asuncion", "America/Atikokan", "America/Atka", "America/Bahia", "America/Bahia_Banderas", "America/Barbados", "America/Belem", "America/Belize", "America/Blanc-Sablon", "America/Boa_Vista", "America/Bogota", "America/Boise", "America/Buenos_Aires", .......... a / Adak”,“美国/安克雷奇”,“美国/安圭拉”,“美国/安提瓜”,“美国/阿拉瓜那”,“美国/阿根廷/布宜诺斯艾利斯”,“美国/阿根廷/卡塔马卡”,“美国/阿根廷/ ComodRivadavia”,“美国/阿根廷/科尔多瓦”,“美国/阿根廷/胡胡伊”,“美国/阿根廷/拉里奥哈”,“美国/阿根廷/门多萨”,“美国/阿根廷/里奥·加勒戈斯”,“美国/阿根廷/萨尔塔” ,“美国/阿根廷/圣胡安”,“美国/阿根廷/圣路易斯”,“美国/阿根廷/土库曼”,“美国/阿根廷/乌斯怀亚”,“美国/阿鲁巴”,“美国/亚松森”,“美国/阿蒂坎坎” ,“美国/阿特卡”,“美国/巴伊亚州”,“美国/巴伊亚州-班德拉斯”,“美国/巴巴多斯”,“美国/贝伦”,“美国/伯利兹”,“美国/布兰克-萨布隆”,“美国/博阿维斯塔” ,“美国/波哥大”,“美国/博伊西”,“美国/布宜诺斯艾利斯”,..........

thank you 谢谢

I don't know of any such list. 我不知道任何这样的清单。 After all, who decides which time zones are the "main timezones"? 毕竟,谁来决定“主要时区”是哪个时区? However, one option is to do what Microsoft Windows does and sort the list by timezone offset: 但是,一种选择是执行Microsoft Windows的操作,然后按时区偏移量对列表进行排序:

屏幕截图

From the moment docs : 文档开始

moment("2016-03-03").tz("America/Toronto").format('Z'); 

will give you the offset of that particular zone, eg, -05:00 . 将为您提供该特定区域的偏移量,例如-05:00 You can get the offset for each zone, sort by offset, and then present the list. 您可以获取每个区域的偏移量,按偏移量排序,然后显示列表。 The text in the list can be the zone name, or the part after the / , with _ replaced with space (eg, America/New_York -> New York ). 列表中的文本可以是区域名称,也可以是/后面的部分,用_代替空格(例如America/New_York > New York )。

As everyone has said, the list is the list. 正如每个人都说的那样,列表就是列表。 What I did to narrow down the list is use a session variable that has the user's timezone, and if that is null, then use a default of America/UK/South Africa/Australian timezones. 我要缩小列表的范围是使用具有用户时区的会话变量,如果该变量为null,则使用默认的America / UK / South Africa / Australian时区。 For my company, this is where the majority of our customers come from. 对于我的公司来说,这是我们大多数客户的来源。 Not many Canadians, strangely enough. 加拿大人不多,很奇怪。

This is all in PHP, and not JavaScript, but it shouldn't be too hard to read and figure out. 所有这些都是PHP而不是JavaScript编写的,但阅读和弄清楚它应该不难。 What's important are the steps used to narrow down the results. 重要的是用来缩小结果范围的步骤。

Here's the gist: 这是要点:

https://gist.github.com/NoMan2000/a262a96b159164882cd7 https://gist.github.com/NoMan2000/a262a96b159164882cd7

The output.html shows you what the default timeZone breakdown is. output.html向您显示默认的timeZone细分是什么。

The most important file is the CountryCodesAndAbbr.php file. 最重要的文件是CountryCodesAndAbbr.php文件。

https://gist.github.com/NoMan2000/a262a96b159164882cd7#file-countrycodesandabbr-php https://gist.github.com/NoMan2000/a262a96b159164882cd7#file-countrycodesandabbr-php

It has an array of five different country ISOs stored in JSON format. 它具有以JSON格式存储的五个不同国家/地区ISO的数组。 It makes a CURL request, responds with the JSON, and then stores that in a file so I don't have to keep re-downloading the files each time, and bombarding the APIs. 它发出一个CURL请求,以JSON响应,然后将其存储在文件中,因此我不必每次都重新下载文件并轰炸API。

For our purposes, only worry about the getContinentAndCountry method, as that contains all the data we'll need. 就我们的目的而言,只需要担心getContinentAndCountry方法,因为该方法包含我们需要的所有数据。 This is what moment does for you in javaScript, with some tweaks to how the API gets called. 这就是在JavaScript中为您做的事情,并对API的调用方式进行了一些调整。

The TimeFormatter is a class for dealing with how PHP internally handles timezones, and I need to translate between the user's country-code and the displayed timezones so I don't overwhelm them. TimeFormatter是用于处理PHP内部如何处理时区的类,我需要在用户的国家/地区代码和显示的时区之间进行转换,以免造成不便。

In PHP, I use the user's country that I get when they sign up, so I have a region to pass into the method to narrow down the results. 在PHP中,我使用他们注册时得到的用户所在的国家/地区,因此我有一个区域可以传入方法以缩小结果范围。 If you don't have a session variable or some other information saved, use this API. 如果您没有保存会话变量或其他一些信息,请使用此API。

http://freegeoip.net http://freegeoip.net

This will give you the country when you make a request to it with the user's IP. 当您使用用户的IP向其提出请求时,这将为您提供国家/地区。 Now you have the country. 现在你有了国家。

If you don't get a result back, then do what I do and default to the regions where you get the most customers, with an optional <select> field that will allow them to pick the country they want. 如果您没有得到结果,请执行我的操作并默认为获得最多客户的地区,并带有一个可选的<select>字段,该字段将允许他们选择所需的国家/地区。

You have to pick whether or not you are going to use the IP or if you have some way of getting a value to the user. 您必须选择是否要使用IP或是否有某种方法可以为用户带来价值。

Lastly, pass in the country to the TimeFormatter->getTimezonesByCountryCodeLookup(region) 最后,将国家/地区传递给TimeFormatter-> getTimezonesByCountryCodeLookup(region)

https://gist.github.com/NoMan2000/a262a96b159164882cd7#file-timeformatter-php https://gist.github.com/NoMan2000/a262a96b159164882cd7#file-timeformatter-php

That takes the 2 letter country code and looks it up. 这需要2个字母的国家/地区代码并进行查找。 The end result is you end up with a much smaller result that user can navigate. 最终结果是您得到的结果要小得多,用户可以浏览。 I combined this with chosen.js and it enabled them to just type in their abbreviation to narrow it down. 我将其与selected.js结合使用,它使他们只需键入缩写即可缩小范围。

You will have to adapt this code to your own uses, but hopefully it shows you how to go about doing it. 您将不得不将此代码修改为适合自己的用途,但希望它可以向您展示如何进行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM