获得城市和城市iOS时区中的国家/地区列表

时间:2017-11-26 08:19:36

标签: ios swift timezone

我希望能够访问设置>中显示的用户友好时区列表。一般>日期&时间。有人可以帮忙为此提供代码吗?

Settings > General > Data & Time > Time Zone (Selection)

3 个答案:

答案 0 :(得分:10)

Swift 4

获取当前用户时区:

let currentTimeZone = TimeZone.current

获取所有时区:

let timeZoneIdentifiers = TimeZone.knownTimeZoneIdentifiers

使用标识符创建时区:

let berlin = TimeZone(identifier: "Europe/Berlin")!

获取时区的城市名称:

let cityName = berlin.identifier.split(separator: "/")[1]

获取所有城市名称:

var allCities: [String] = []
    for identifier in timeZoneIdentifiers {
    if let cityName = identifier.split(separator: "/").last {
        allCities.append("\(cityName)")
    }
}

答案 1 :(得分:1)

试试这个:

let timezone = TimeZone.current.identifier

这将返回“America / Sao_Paulo”。

抱歉,这将获得所有列表:

let array = TimeZone.knownTimeZoneIdentifiers

答案 2 :(得分:-2)

ADT = "America/Halifax";
AKDT = "America/Juneau";
AKST = "America/Juneau";
ART = "America/Argentina/Buenos_Aires";
AST = "America/Halifax";
BDT = "Asia/Dhaka";
BRST = "America/Sao_Paulo";
BRT = "America/Sao_Paulo";
BST = "Europe/London";
CAT = "Africa/Harare";
CDT = "America/Chicago";
CEST = "Europe/Paris";
CET = "Europe/Paris";
CLST = "America/Santiago";
CLT = "America/Santiago";
COT = "America/Bogota";
CST = "America/Chicago";
EAT = "Africa/Addis_Ababa";
EDT = "America/New_York";
EEST = "Europe/Istanbul";
EET = "Europe/Istanbul";
EST = "America/New_York";
GMT = GMT;
GST = "Asia/Dubai";
HKT = "Asia/Hong_Kong";
HST = "Pacific/Honolulu";
ICT = "Asia/Bangkok";
IRST = "Asia/Tehran";
IST = "Asia/Calcutta";
JST = "Asia/Tokyo";
KST = "Asia/Seoul";
MDT = "America/Denver";
MSD = "Europe/Moscow";
MSK = "Europe/Moscow";
MST = "America/Denver";
NZDT = "Pacific/Auckland";
NZST = "Pacific/Auckland";
PDT = "America/Los_Angeles";
PET = "America/Lima";
PHT = "Asia/Manila";
PKT = "Asia/Karachi";
PST = "America/Los_Angeles";
SGT = "Asia/Singapore";
UTC = UTC;
WAT = "Africa/Lagos";
WEST = "Europe/Lisbon";
WET = "Europe/Lisbon";
WIT = "Asia/Jakarta";