3

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.

HenryRootTwo
  • 2,572
  • 1
  • 27
  • 27

3 Answers3

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

This is what you're looking for.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
2

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.

Daniel Lyon
  • 1,499
  • 10
  • 15
  • 1
    What's the point of the `DateFormatter` here? All you need is the `TimeZone`. And the question is asking about the users's current timezone. – rmaddy Jul 27 '18 at 15:32
  • Thats true it just happened to be there in my playground. I will remove it. – Daniel Lyon Jul 27 '18 at 19:12
0

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.

  • 1
    This is not the requested result. – rmaddy Jul 27 '18 at 02:45
  • My bad. For HenryRootTwo's "NZST", the result would have been "New Zealand Standard Time" instead of "Pacific/Auckland". I like how you used the identifier property, but I think he needs TimeZone.current.identifier, because in his original code he's trying to retrieve not a specific time zone, but whatever is the user's time zone. – Robot Kate Jul 27 '18 at 02:58