Reputation: 2612
How do I get a String, "Pacific/Auckland" for example, from a user's TimeZone?
I can get the "NZST" abbreviation with
TimeZone.current.abbreviation()
But it's the full name I need, if I can get it.
Upvotes: 3
Views: 3747
Reputation: 1589
One of these should work for you.
timeZone = TimeZone(abbreviation: "EST")
print(timeZone) // "some(America/New_York (current))\n"
print(timeZone.abbreviation()) // "Optional("EDT")\n"
print(timeZone.description) // "America/New_York (current)\n"
print(timeZone.identifier) // "America/New_York\n"
I believe you are looking for the last one.
Upvotes: 2
Reputation: 131
let timeZone = TimeZone(abbreviation: "NZST")
let identifier = timeZone?.identifier
This is what you're looking for.
Upvotes: 3
Reputation: 1
Try writing...
TimeZone.current.localizedName(for: .standard, locale: Locale.current)
In the first parameter, you can select different name styles. The only two I've tried out are .generic and .standard.
I found the answer here: How to get a user's time zone?
I hope that helps.
Upvotes: 0