TimeZone电流不起作用

时间:2018-03-28 04:40:33

标签: swift nsdateformatter

我遇到了从String获取正确日期时间的问题。

let timeStamp = "2018-03-27 21:10:55 PDT"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss zzz"
//Los Angeles is current device timezone
dateFormatter.timeZone = TimeZone.current 
let date = dateFormatter.date(from: timeStamp)
print(date) //2018-03-28 04:10:55 +0000

有没有办法可以在设备时区获取日期?例如,我没有得到2018-03-28 04:10:55 +0000,而是希望在设备时区获得时间。

1 个答案:

答案 0 :(得分:0)

获取时区:

let timezone = TimeZone.current.identifier
print(timezone)

简短地说:

let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

//设置缩写“EST”,“BDT”

formatter.timeZone = TimeZone(abbreviation: "BDT")

//在此处设置您的设备时间

let estDate = formatter.date(from: "2018-03-28 11:28:00")
print("\(estDate!)")

outPut是:

Asia/Dhaka

通过使用此功能,您将获得所有:

let knownTimeZoneIdentifiers = TimeZone.knownTimeZoneIdentifiers
for tz in TimeZone.knownTimeZoneIdentifiers {
    let timeZone = TimeZone(identifier: tz)
    if let abbreviation = timeZone?.abbreviation(), let seconds = timeZone?.secondsFromGMT() {
        print ("timeZone: \(tz) \nabbreviation: \(abbreviation)\nsecondsFromGMT: \(seconds)\n")
    }
}

输出:

timeZone: Africa/Abidjan 
abbreviation: GMT
secondsFromGMT: 0

timeZone: Africa/Accra 
abbreviation: GMT
secondsFromGMT: 0

timeZone: Africa/Addis_Ababa 
abbreviation: GMT+3
secondsFromGMT: 10800

timeZone: Africa/Algiers 
abbreviation: GMT+1
secondsFromGMT: 3600

timeZone: Africa/Asmara 
abbreviation: GMT+3
secondsFromGMT: 10800

timeZone: Africa/Bamako 
abbreviation: GMT
secondsFromGMT: 0

timeZone: Africa/Bangui 
abbreviation: GMT+1
secondsFromGMT: 3600

timeZone: Africa/Banjul 
abbreviation: GMT
secondsFromGMT: 0

timeZone: Africa/Bissau 
abbreviation: GMT
secondsFromGMT: 0

timeZone: Africa/Blantyre 
abbreviation: GMT+2
secondsFromGMT: 7200

timeZone: Africa/Brazzaville 
abbreviation: GMT+1
secondsFromGMT: 3600

timeZone: Africa/Bujumbura 
abbreviation: GMT+2
secondsFromGMT: 7200

timeZone: Africa/Cairo 
abbreviation: GMT+2
secondsFromGMT: 7200

timeZone: Africa/Casablanca 
abbreviation: GMT+1
secondsFromGMT: 3600

timeZone: Africa/Ceuta 
abbreviation: GMT+2
secondsFromGMT: 7200

timeZone: Africa/Conakry 
abbreviation: GMT
secondsFromGMT: 0

timeZone: Africa/Dakar 
abbreviation: GMT
secondsFromGMT: 0

timeZone: Africa/Dar_es_Salaam 
abbreviation: GMT+3
secondsFromGMT: 10800

timeZone: Africa/Djibouti 
abbreviation: GMT+3
secondsFromGMT: 10800

timeZone: Africa/Douala 
abbreviation: GMT+1
secondsFromGMT: 3600

timeZone: Africa/El_Aaiun 
abbreviation: GMT+1
secondsFromGMT: 3600

timeZone: Africa/Freetown 
abbreviation: GMT
secondsFromGMT: 0

timeZone: Africa/Gaborone 
abbreviation: GMT+2
secondsFromGMT: 7200

timeZone: Africa/Harare 
abbreviation: GMT+2
secondsFromGMT: 7200

timeZone: Africa/Johannesburg 
abbreviation: GMT+2
secondsFromGMT: 7200

timeZone: Africa/Juba 
abbreviation: GMT+3
secondsFromGMT: 10800

timeZone: Africa/Kampala 
abbreviation: GMT+3
secondsFromGMT: 10800

timeZone: Africa/Khartoum 
abbreviation: GMT+2
secondsFromGMT: 7200

timeZone: Africa/Kigali 
abbreviation: GMT+2
secondsFromGMT: 7200

timeZone: Africa/Kinshasa 
abbreviation: GMT+1
secondsFromGMT: 3600

timeZone: Africa/Lagos 
abbreviation: GMT+1
secondsFromGMT: 3600

timeZone: Africa/Libreville 
abbreviation: GMT+1
secondsFromGMT: 3600

timeZone: Africa/Lome 
abbreviation: GMT
secondsFromGMT: 0

timeZone: Africa/Luanda 
abbreviation: GMT+1
secondsFromGMT: 3600

timeZone: Africa/Lubumbashi 
abbreviation: GMT+2
secondsFromGMT: 7200

timeZone: Africa/Lusaka 
abbreviation: GMT+2
secondsFromGMT: 7200

timeZone: Africa/Malabo 
abbreviation: GMT+1
secondsFromGMT: 3600

timeZone: Africa/Maputo 
abbreviation: GMT+2
secondsFromGMT: 7200

timeZone: Africa/Maseru 
abbreviation: GMT+2
secondsFromGMT: 7200

timeZone: Africa/Mbabane 
abbreviation: GMT+2
secondsFromGMT: 7200

timeZone: Africa/Mogadishu 
abbreviation: GMT+3
secondsFromGMT: 10800

timeZone: Africa/Monrovia 
abbreviation: GMT
secondsFromGMT: 0

timeZone: Africa/Nairobi 
abbreviation: GMT+3
secondsFromGMT: 10800

timeZone: Africa/Ndjamena 
abbreviation: GMT+1
secondsFromGMT: 3600

timeZone: Africa/Niamey 
abbreviation: GMT+1
secondsFromGMT: 3600

timeZone: Africa/Nouakchott 
abbreviation: GMT
secondsFromGMT: 0

timeZone: Africa/Ouagadougou 
abbreviation: GMT
secondsFromGMT: 0

timeZone: Africa/Porto-Novo 
abbreviation: GMT+1
secondsFromGMT: 3600

timeZone: Africa/Sao_Tome 
abbreviation: GMT+1
secondsFromGMT: 3600

timeZone: Africa/Tripoli 
abbreviation: GMT+2
secondsFromGMT: 7200

timeZone: Africa/Tunis 
abbreviation: GMT+1
secondsFromGMT: 3600

timeZone: Africa/Windhoek 
abbreviation: GMT+2
secondsFromGMT: 7200

timeZone: America/Adak 
abbreviation: HADT
secondsFromGMT: -32400

timeZone: America/Anchorage 
abbreviation: AKDT
secondsFromGMT: -28800

timeZone: America/Anguilla 
abbreviation: AST
secondsFromGMT: -14400

timeZone: America/Antigua 
abbreviation: AST
secondsFromGMT: -14400

timeZone: America/Araguaina 
abbreviation: GMT-3
secondsFromGMT: -10800

timeZone: America/Argentina/Buenos_Aires 
abbreviation: GMT-3
secondsFromGMT: -10800

timeZone: America/Argentina/Catamarca 
abbreviation: GMT-3
secondsFromGMT: -10800

timeZone: America/Argentina/Cordoba 
abbreviation: GMT-3
secondsFromGMT: -10800

timeZone: America/Argentina/Jujuy 
abbreviation: GMT-3
secondsFromGMT: -10800

timeZone: America/Argentina/La_Rioja 
abbreviation: GMT-3
secondsFromGMT: -10800

timeZone: America/Argentina/Mendoza 
abbreviation: GMT-3
secondsFromGMT: -10800

timeZone: America/Argentina/Rio_Gallegos 
abbreviation: GMT-3
secondsFromGMT: -10800

timeZone: America/Argentina/Salta 
abbreviation: GMT-3
secondsFromGMT: -10800

timeZone: America/Argentina/San_Juan 
abbreviation: GMT-3
secondsFromGMT: -10800

timeZone: America/Argentina/San_Luis 
abbreviation: GMT-3
secondsFromGMT: -10800

timeZone: America/Argentina/Tucuman 
abbreviation: GMT-3
secondsFromGMT: -10800

timeZone: America/Argentina/Ushuaia 
abbreviation: GMT-3
secondsFromGMT: -10800

timeZone: America/Aruba 
abbreviation: AST
secondsFromGMT: -14400

timeZone: America/Asuncion 
abbreviation: GMT-4
secondsFromGMT: -14400

timeZone: America/Atikokan 
abbreviation: EST
secondsFromGMT: -18000

timeZone: America/Bahia 
abbreviation: GMT-3
secondsFromGMT: -10800

timeZone: America/Bahia_Banderas 
abbreviation: CST
secondsFromGMT: -21600

timeZone: America/Barbados 
abbreviation: AST
secondsFromGMT: -14400

timeZone: America/Belem 
abbreviation: GMT-3
secondsFromGMT: -10800

timeZone: America/Belize 
abbreviation: CST
secondsFromGMT: -21600

timeZone: America/Blanc-Sablon 
abbreviation: AST
secondsFromGMT: -14400

timeZone: America/Boa_Vista 
abbreviation: GMT-4
secondsFromGMT: -14400

timeZone: America/Bogota 
abbreviation: GMT-5
secondsFromGMT: -18000

timeZone: America/Boise 
abbreviation: MDT
secondsFromGMT: -21600

timeZone: America/Cambridge_Bay 
abbreviation: MDT
secondsFromGMT: -21600

timeZone: America/Campo_Grande 
abbreviation: GMT-4
secondsFromGMT: -14400

timeZone: America/Cancun 
abbreviation: EST
secondsFromGMT: -18000

timeZone: America/Caracas 
abbreviation: GMT-4
secondsFromGMT: -14400

timeZone: America/Cayenne 
abbreviation: GMT-3
secondsFromGMT: -10800

timeZone: America/Cayman 
abbreviation: EST
secondsFromGMT: -18000

timeZone: America/Chicago 
abbreviation: CDT
secondsFromGMT: -18000

timeZone: America/Chihuahua 
abbreviation: GMT-7
secondsFromGMT: -25200

timeZone: America/Costa_Rica 
abbreviation: CST
secondsFromGMT: -21600

timeZone: America/Creston 
abbreviation: MST
secondsFromGMT: -25200

timeZone: America/Cuiaba 
abbreviation: GMT-4
secondsFromGMT: -14400

timeZone: America/Curacao 
abbreviation: AST
secondsFromGMT: -14400

timeZone: America/Danmarkshavn 
abbreviation: GMT
secondsFromGMT: 0

timeZone: America/Dawson 
abbreviation: PDT
secondsFromGMT: -25200

timeZone: America/Dawson_Creek 
abbreviation: MST
secondsFromGMT: -25200

timeZone: America/Denver 
abbreviation: MDT
secondsFromGMT: -21600

timeZone: America/Detroit 
abbreviation: EDT
secondsFromGMT: -14400

timeZone: America/Dominica 
abbreviation: AST
secondsFromGMT: -14400

timeZone: America/Edmonton 
abbreviation: MDT
secondsFromGMT: -21600

timeZone: America/Eirunepe 
abbreviation: GMT-5
secondsFromGMT: -18000

timeZone: America/El_Salvador 
abbreviation: CST
secondsFromGMT: -21600

timeZone: America/Fort_Nelson 
abbreviation: MST
secondsFromGMT: -25200

timeZone: America/Fortaleza 
abbreviation: GMT-3
secondsFromGMT: -10800

timeZone: America/Glace_Bay 
abbreviation: ADT
secondsFromGMT: -10800

timeZone: America/Godthab 
abbreviation: GMT-2
secondsFromGMT: -7200

timeZone: America/Goose_Bay 
abbreviation: ADT
secondsFromGMT: -10800

timeZone: America/Grand_Turk 
abbreviation: EDT
secondsFromGMT: -14400

timeZone: America/Grenada 
abbreviation: AST
secondsFromGMT: -14400

timeZone: America/Guadeloupe 
abbreviation: AST
secondsFromGMT: -14400