【问题标题】:What is the proper format for setting TimeZones in iOS ?在 iOS 中设置 TimeZones 的正确格式是什么?
【发布时间】:2016-04-12 14:44:56
【问题描述】:

我已尝试使用此 Wiki 来获取要使用的时区的适当缩写:

let dateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = formatString
        dateFormatter.timeZone = NSTimeZone(abbreviation: setZone)

“GMT”和“GST”一样有效,但由于某些原因,大部分缩写词不起作用(例如 GYT)。我应该使用其他格式吗?我也尝试过传入 UTC-X,但这也没有用

【问题讨论】:

  • 使用采用forSecondsFromGMTNSTimeZone初始化器。
  • 据我了解,NSTimeZone forSecondsFromGMT 需要计算偏移加班时间我想更改显示的时区(在此应用程序中经常发生)是正确的 IE .. GMT +6 = 60*6 = 360 秒 [NSTimeZone forSeecondsFromGMT:360]
  • 那是几分钟。 GMT+6 表示 6 小时。所以这是 6*60*60 秒。
  • :) 是的,抱歉……这是我在会议上半关注的时候发布的结果:)

标签: ios swift nstimezone


【解决方案1】:

这是我从 NSTimer 调用的函数...

self.clock = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(ViewController.updateClock), userInfo: nil, repeats: true)

func updateClock() {
      dispatch_async(dispatch_get_main_queue()) {
        let date = NSDate();
        // "Apr 1, 2015, 8:53 AM" <-- local without seconds

        var formatter = NSDateFormatter();
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

        _ = formatter.stringFromDate(date);
                    // "2015-04-01 08:52:00 -0400" <-- same date, local, but with seconds
        formatter.timeZone = NSTimeZone(abbreviation: "CET");
        let cetTimeZoneStr = formatter.stringFromDate(date);
        // "2015-04-01 12:52:00 +0000" <-- same date, now in UTC

        self.timer.text = cetTimeZoneStr


    }
}

这里有一些代码可以让系统知道时区!

let blah = NSTimeZone.knownTimeZoneNames()
let blah2 = NSTimeZone.abbreviationDictionary()
print("\(blah) \(blah2)")

【讨论】:

  • 这如何回答这个问题?
  • rmaddy,我重读了这个问题;回答得太快感到内疚,做了一些谷歌搜索并找到了答案,我将其添加到我的答案中:)
  • 我很感激 - 我使用了 abbreviationDict 而不是我创建的 .plist。谢谢
猜你喜欢
  • 1970-01-01
  • 2013-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-23
  • 2014-01-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多