【问题标题】:Time zone abbreviation not returning it in double digit format时区缩写不以两位数格式返回
【发布时间】:2018-03-28 19:01:51
【问题描述】:
TimeZone.current.abbreviation() 

它将返回 GMT+5:30 ,我的要求是让它为 GMT+05:30

【问题讨论】:

    标签: ios swift timezone


    【解决方案1】:

    如果您需要特定格式的当前时区,一种方法是使用DateFormatter 以一种有些非正统的方式:

    // formatter.timeZone is implicitly TimeZone.current
    let formatter = DateFormatter()
    formatter.dateFormat = "ZZZZ"
    
    let abbreviation = formatter.string(from: Date())
    

    这会返回一个格式为“GMT±xx:yy”的字符串;对我来说,这是“GMT-07:00”。

    请参阅Date Field Symbol Table from TR35-31 了解更多信息。

    【讨论】:

      【解决方案2】:

      远离我的编译器进行测试,但这应该可以解决问题。

      let tz = TimeZone.current.abbreviation() 
      if tz.contains(“+11”) || tz.contains(“+12”) {
      // do nothing
      } else {
          tz.replaceAlOcurrences(of: “+”,  with: “+0”)
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-31
        • 2023-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多