错了NTP日期响应斯威夫特4

问题描述 投票:-1回答:1

我使用Kronos从NTP服务器获取日期,问题是,返回的日期是错误的。

例如:在我的国家的时间是2019年2月2日20时22分05秒+0000但我得到的是2019年2月2日22时22分05秒+0000

我认为这是对的时区,但我不能达到它

谢谢。

ios swift date time ntp
1个回答
0
投票

我写了一些代码,使其正确

Clock.sync { date, offset in
        let dateFormatter = DateFormatter()
        dateFormatter.calendar = NSCalendar.current
        dateFormatter.timeZone = TimeZone.current
        dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

        let dt = dateFormatter.string(from: date!)
        dateFormatter.timeZone = TimeZone(abbreviation: "UTC")

        print(dt)
    }
© www.soinside.com 2019 - 2024. All rights reserved.