swift中utc时区和设备时区之间的区别

0 swift3

我正在开发一个聊天应用程序.我想确定UTC时区和设备当前时区之间的时差.如何在swift3中做到这一点?

phi*_*kin 9

 // Returns a Date initialized to the current date and time
 let currentDate = Date()

 // Returns the time zone currently used by the system
 let timeZone = NSTimeZone.system

 // Returns the difference in seconds between the receiver and Greenwich Mean Time at a given date.
 let timeZoneOffset = timeZone.secondsFromGMT(for: currentDate) / 3600

 print(timeZoneOffset, "hours offset for timezone", timeZone)

 // example output for Denver (considers daylight saving time)
 // -6  hours offset for timezone  America/Denver (current)
Run Code Online (Sandbox Code Playgroud)