HenryRootTwo
HenryRootTwo

Reputation: 2612

Swift iOS TimeZone as String?

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

Answers (3)

Daniel Lyon
Daniel Lyon

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

let timeZone = TimeZone(abbreviation: "NZST")
let identifier = timeZone?.identifier

This is what you're looking for.

Upvotes: 3

Robot Kate
Robot Kate

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

Related Questions