Does anyone know of a way to get a users time zone in Swift?
I'm getting a specific time something is on t.v. out of a database and then need to subtract/add from where they are located to show them the correct time it's on.
Does anyone know of a way to get a users time zone in Swift?
I'm getting a specific time something is on t.v. out of a database and then need to subtract/add from where they are located to show them the correct time it's on.
Xcode 8.2.1 • Swift 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"
To retrieve the current user time zone, use the code below.
TimeZone.current.abbreviation()!
Other Date Formate here.
OUTPUT:
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
NSString *tzName = [timeZone name];
let tzName = TimeZone.current.identifier
The name will be something like "Australia/Sydney", or "Europe/Lisbon".
Since it sounds like you might only care about the continent, that might be all you need.
Swift 4, 4.2 & 5
var gmtHoursOffset : String = String()
override func viewDidLoad() {
super.viewDidLoad()
gmtHoursOffset = getHoursFromGmt()
print(gmtHoursOffset)
}
func getHoursFromGmt() -> String {
let secondsFromGmt: Int = TimeZone.current.secondsFromGMT()
let hoursFromGmt = (secondsFromGmt / 3600)
return "\(hoursFromGmt)"
}
edit/update:
Xcode 8 or later • Swift 3 or later
if you need the abbreviation:
if you need the timezone identifier:
To know all timezones abbreviations available:
To know all timezones names (identifiers) available:
There is a few other info you may need:
TimeZone - Apple Developer Swift Documentation