【问题标题】:iOS TimeZone get three letter abbreviationiOS TimeZone 获取三个字母的缩写
【发布时间】:2019-10-23 12:35:23
【问题描述】:

我需要获得名为 'Africa/Nairobi' 的国家/地区的三/四个字母缩写,我已尝试使用

print(TimeZone(abbreviation: "SAST")?.identifier) 来自NSTimeZone.abbreviationDictionary,结果nil

如何获得这个国家的三/四字母缩写'Africa/Nairobi'。提前致谢

【问题讨论】:

    标签: ios timezone nsdate nstimezone


    【解决方案1】:

    您可以通过以下方法获取完整的标准本地化时区名称。

    let timezone:TimeZone = TimeZone.init(identifier: "Africa/Nairobi") ?? TimeZone.current
    print(timezone.localizedName(for: .generic, locale: .autoupdatingCurrent))
    print(timezone.localizedName(for: .standard, locale: .autoupdatingCurrent))
    

    会给你以下输出

    可选(“东非时间”)

    可选(“东非时间”)

    我认为现在您可以通过拆分和组合字符串轻松获得东非时间的 EAT

    let fullName = timezone.localizedName(for: .standard, locale: .autoupdatingCurrent) ?? ""
    var result = ""
    fullName.enumerateSubstrings(in: fullName.startIndex..<fullName.endIndex, options: .byWords) { (substring, _, _, _) in
        if let substring = substring { result += substring.prefix(1) }
    }
    print(result)
    

    会给你放出来

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-18
      • 1970-01-01
      • 1970-01-01
      • 2012-07-04
      • 1970-01-01
      • 1970-01-01
      • 2017-12-29
      相关资源
      最近更新 更多