我正在开发一款聊天应用。我想确定UTC时区和设备当前时区之间的时差。如何在swift3中做到这一点?
答案 0 :(得分:7)
// 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)