64
A
回答
166
編輯/更新:
的Xcode 8•斯威夫特3
var secondsFromGMT: Int { return TimeZone.current.secondsFromGMT() }
secondsFromGMT // -7200
,如果你需要的縮寫,你可以這樣做:
var localTimeZoneAbbreviation: String { return TimeZone.current.abbreviation() ?? "" }
localTimeZoneAbbreviation // "GMT-2"
如果你需要你的名字可以這樣做:
var localTimeZoneName: String { return TimeZone.current.identifier }
localTimeZoneName // "America/Sao_Paulo"
如果你想知道的所有時區使用縮寫,可以,你可以這樣做:
var timeZoneAbbreviations: [String:String] { return TimeZone.abbreviationDictionary }
timeZoneAbbreviations // ["CEST": "Europe/Paris", "WEST": "Europe/Lisbon", "CDT": "America/Chicago", "EET": "Europe/Istanbul", "BRST": "America/Sao_Paulo", "EEST": "Europe/Istanbul", "CET": "Europe/Paris", "MSD": "Europe/Moscow", "MST": "America/Denver", "KST": "Asia/Seoul", "PET": "America/Lima", "NZDT": "Pacific/Auckland", "CLT": "America/Santiago", "HST": "Pacific/Honolulu", "MDT": "America/Denver", "NZST": "Pacific/Auckland", "COT": "America/Bogota", "CST": "America/Chicago", "SGT": "Asia/Singapore", "CAT": "Africa/Harare", "BRT": "America/Sao_Paulo", "WET": "Europe/Lisbon", "IST": "Asia/Calcutta", "HKT": "Asia/Hong_Kong", "GST": "Asia/Dubai", "EDT": "America/New_York", "WIT": "Asia/Jakarta", "UTC": "UTC", "JST": "Asia/Tokyo", "IRST": "Asia/Tehran", "PHT": "Asia/Manila", "AKDT": "America/Juneau", "BST": "Europe/London", "PST": "America/Los_Angeles", "ART": "America/Argentina/Buenos_Aires", "PDT": "America/Los_Angeles", "WAT": "Africa/Lagos", "EST": "America/New_York", "BDT": "Asia/Dhaka", "CLST": "America/Santiago", "AKST": "America/Juneau", "ADT": "America/Halifax", "AST": "America/Halifax", "PKT": "Asia/Karachi", "GMT": "GMT", "ICT": "Asia/Bangkok", "MSK": "Europe/Moscow", "EAT": "Africa/Addis_Ababa"]
如果你想知道的所有時區的名稱可用,你可以這樣做:
var timeZoneIdentifiers: [String] { return TimeZone.knownTimeZoneIdentifiers }
timeZoneIdentifiers // ["Africa/Abidjan", "Africa/Accra", "Africa/Addis_Ababa", "Africa/Algiers", "Africa/Asmara", "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/Tripoli", "Africa/Tunis", "Africa/Windhoek", "America/Adak", "America/Anchorage", "America/Anguilla", "America/Antigua", "America/Araguaina", "America/Argentina/Buenos_Aires", "America/Argentina/Catamarca", "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/Bahia", "America/Bahia_Banderas", "America/Barbados", "America/Belem", "America/Belize", "America/Blanc-Sablon", "America/Boa_Vista", "America/Bogota", …, "Pacific/Marquesas", "Pacific/Midway", "Pacific/Nauru", "Pacific/Niue", "Pacific/Norfolk", "Pacific/Noumea", "Pacific/Pago_Pago", "Pacific/Palau", "Pacific/Pitcairn", "Pacific/Pohnpei", "Pacific/Ponape", "Pacific/Port_Moresby", "Pacific/Rarotonga", "Pacific/Saipan", "Pacific/Tahiti", "Pacific/Tarawa", "Pacific/Tongatapu", "Pacific/Truk", "Pacific/Wake", "Pacific/Wallis"]
有在一些其他的信息,你可能需要:
var isDaylightSavingTime: Bool { return TimeZone.current.isDaylightSavingTime(for: Date()) }
print(isDaylightSavingTime) // true (in effect)
var daylightSavingTimeOffset: TimeInterval { return TimeZone.current.daylightSavingTimeOffset() }
print(daylightSavingTimeOffset) // 3600 seconds (1 hour - daylight savings time)
var nextDaylightSavingTimeTransition: Date? { return TimeZone.current.nextDaylightSavingTimeTransition } // "Feb 18, 2017, 11:00 PM"
print(nextDaylightSavingTimeTransition?.description(with: .current) ?? "none")
nextDaylightSavingTimeTransition // "Saturday, February 18, 2017 at 11:00:00 PM Brasilia Standard Time\n"
var nextDaylightSavingTimeTransitionAfterNext: Date? {
guard
let nextDaylightSavingTimeTransition = nextDaylightSavingTimeTransition
else { return nil }
return TimeZone.current.nextDaylightSavingTimeTransition(after: nextDaylightSavingTimeTransition)
}
nextDaylightSavingTimeTransitionAfterNext // "Oct 15, 2017, 1:00 AM"
11
的Xcode 8.2.1•斯威夫特3.0.2
Locale.availableIdentifiers
Locale.isoRegionCodes
Locale.isoCurrencyCodes
Locale.isoLanguageCodes
Locale.commonISOCurrencyCodes
Locale.current.regionCode // "US"
Locale.current.languageCode // "en"
Locale.current.currencyCode // "USD"
Locale.current.currencySymbol // "$"
Locale.current.groupingSeparator // ","
Locale.current.decimalSeparator // "."
Locale.current.usesMetricSystem // false
Locale.windowsLocaleCode(fromIdentifier: "pt_BR") // 1,046
Locale.identifier(fromWindowsLocaleCode: 1046) ?? "" // "pt_BR"
Locale.windowsLocaleCode(fromIdentifier: Locale.current.identifier) // 1,033 Note: I am in Brasil but I use "en_US" format with all my devices
Locale.windowsLocaleCode(fromIdentifier: "en_US") // 1,033
Locale.identifier(fromWindowsLocaleCode: 1033) ?? "" // "en_US"
Locale(identifier: "en_US_POSIX").localizedString(forLanguageCode: "pt") // "Portuguese"
Locale(identifier: "en_US_POSIX").localizedString(forRegionCode: "br") // "Brazil"
Locale(identifier: "en_US_POSIX").localizedString(forIdentifier: "pt_BR") // "Portuguese (Brazil)"
TimeZone.current.localizedName(for: .standard, locale: .current) ?? "" // "Brasilia Standard Time"
TimeZone.current.localizedName(for: .shortStandard, locale: .current) ?? "" // "GMT-3
TimeZone.current.localizedName(for: .daylightSaving, locale: .current) ?? "" // "Brasilia Summer Time"
TimeZone.current.localizedName(for: .shortDaylightSaving, locale: .current) ?? "" // "GMT-2"
TimeZone.current.localizedName(for: .generic, locale: .current) ?? "" // "Brasilia Time"
TimeZone.current.localizedName(for: .shortGeneric, locale: .current) ?? "" // "Sao Paulo Time"
var timeZone: String {
return TimeZone.current.localizedName(for: TimeZone.current.isDaylightSavingTime() ?
.daylightSaving :
.standard,
locale: .current) ?? "" }
timeZone // "Brasilia Summer Time"
0
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
NSString *tzName = [timeZone name];
的名字會像 「澳大利亞/悉尼」,或者 「歐洲/里斯本」。
既然聽起來好像你只關心大陸,那可能就是你需要的一切。
+1
Swift 3相當於TimeZone.current.identifier – Anand 2017-08-18 12:23:51
相關問題
- 1. 如何獲取客戶的時區?
- 2. 如何獲取客戶端時區ID?
- 3. 獲取用戶時區並將utc轉換爲用戶時區
- 4. 如何使用freegeoip.net的API獲取用戶的時區?
- 5. 獲取Exchange Server的用戶時區
- 6. 在用戶的時區獲取用戶的日期和時間
- 7. 在SugarCRM API中,如何獲取用戶的時區信息?
- 8. 如何在JIRA API中獲取用戶的時區?
- 9. 如何獲取訪客用戶的時區
- 10. Javascript:如何根據設備獲取用戶的時區?
- 11. 如何獲取時區
- 12. 如何獲取時區值
- 13. 獲取用戶選擇的時區沒有時區
- 14. 如何使用jquery獲取用戶時區?
- 15. Telegram Bot。獲取用戶時區
- 16. 如何使用FindSystemTimeZoneById獲取元時區的時區?
- 17. 如何在c#中獲取用戶本地機時區#
- 18. 如何使用Boost獲取POSIX時區?
- 19. 如何獲得客戶端的時區
- 20. 如何獲取訪問我的網站的用戶的當前時間/時區?
- 21. 獲取用戶時獲取用戶組
- 22. 如何獲取客戶端JavaScript中的時區?
- 23. 如何獲取任意時區的TIME_ZONE_INFORMATION?
- 24. 如何獲取服務器的時區?
- 25. 如何獲取時區的縮寫?
- 26. 如何獲得客戶端時區
- 27. 如何從Google OAuth2獲取用戶國家和時區信息PHP客戶端
- 28. 根據用戶的時區獲取時間 - PHP
- 29. 獲取用戶時區以顯示正確的時間
- 30. 如何從當前時間獲取獲取時區?
謝謝!這正是我需要的。 – user3712837 2014-11-21 03:31:25
歡迎您 – 2014-11-21 03:34:13
對不起,您可以跳回來,但是您知道打印出不含縮寫名稱的方法嗎?我查看了文檔,但只發現了obj c timeZoneWithName?謝謝。 – user3712837 2014-12-02 00:18:01