【发布时间】:2017-07-19 20:53:40
【问题描述】:
为什么当用户在美国时,此代码返回时区为空......? 在其他国家/地区完美运行。
// Get local time zone
let localTimeZoneAbbreviation = TimeZone.current.abbreviation() ?? ""
let indexStartOfText = localTimeZoneAbbreviation.index(localTimeZoneAbbreviation.startIndex, offsetBy: 3) // 3
let timeZone = localTimeZoneAbbreviation.substring(from: indexStartOfText)
print("TimeZone:",timeZone)
我需要数字格式的结果:
“-7”代表洛杉矶
“-4”代表纽约
汉堡“+2”
等等
【问题讨论】:
-
你为什么要解析字符串?使用
TimeZone的secondsFromGMT方法。 -
如何从 secondsFromGMT 中提取我需要的内容,并且准确地说是在世界各地
-
可以使用 DateFormatter 并设置
dateFormat = "x" -
let dateFormatter = DateFormatter() dateFormatter.dateFormat = "x" dateFormatter.string(from: Date()) // "-03" -
@Leo 是的,它需要不带 0。在您的示例中
标签: swift timezone location mapkit