【问题标题】:Swift - How do I get an NSTimeZone to give me "Pacific Standard Time" or "Mountain Time" etc?Swift - 如何让 NSTimeZone 给我“太平洋标准时间”或“山地时间”等?
【发布时间】:2021-06-28 01:05:06
【问题描述】:

我使用的 API 给了我“美国/温哥华”之类的名称。我用它来构造一个 NSTimeZone 并获得缩写,但当我想要“太平洋夏令时间”或“PDT”时,它是“UTC-7”。

我怎样才能进入该区域?而不是危险区域。

【问题讨论】:

    标签: ios swift timezone nstimezone


    【解决方案1】:

    您可以使用TimeZone 方法localizedName(for: locale:) 来显示本地化的时区名称。您只需要通过名称样式夏令时或标准。如果您需要缩写,只需通过我在post 中显示的短夏令时或短标准:


    extension TimeZone {
        static let vancouver = TimeZone(identifier: "America/Vancouver")!
        func localizedName(for date: Date = Date()) -> String { localizedName(for: isDaylightSavingTime(for: date) ? .daylightSaving : .standard, locale: .current) ?? "" }
        func localizedNameShort(for date: Date = Date()) -> String { localizedName(for: isDaylightSavingTime(for: date) ? .shortDaylightSaving : .shortStandard, locale: .current) ?? "" }
    }
    

    用法:

    TimeZone.vancouver.localizedName()        // "Pacific Daylight Time"
    TimeZone.vancouver.localizedNameShort()   // "PDT"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-22
      • 2022-10-13
      • 2020-02-04
      • 2011-03-13
      • 2020-11-26
      • 1970-01-01
      • 1970-01-01
      • 2012-05-05
      相关资源
      最近更新 更多