代码之家  ›  专栏  ›  技术社区  ›  HenryRootTwo

iOS时区是否为字符串?

  •  2
  • HenryRootTwo  · 技术社区  · 8 年前

    如何从用户的时区获取字符串“Pacific/Auckland”?

    我可以用

    TimeZone.current.abbreviation()
    

    但如果我能拿到的话,这是我需要的全名。

    3 回复  |  直到 8 年前
        1
  •  2
  •   Daniel Lyon    8 年前

    其中一个应该对你有用。

    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"
    

    我相信你在找最后一个。

        2
  •  1
  •   rmaddy    8 年前
    let timeZone = TimeZone(abbreviation: "NZST")
    let identifier = timeZone?.identifier
    

    这就是你要找的。

        3
  •  0
  •   Robot Kate    8 年前

    试着写。。。

    TimeZone.current.localizedName(for: .standard, locale: Locale.current)
    

    在第一个参数中,可以选择不同的名称样式。我只试过两种。普通型和标准型。

    我在这里找到了答案: How to get a user's time zone?

    我希望这会有帮助。