【发布时间】:2018-03-28 19:01:51
【问题描述】:
TimeZone.current.abbreviation()
它将返回 GMT+5:30 ,我的要求是让它为 GMT+05:30
【问题讨论】:
TimeZone.current.abbreviation()
它将返回 GMT+5:30 ,我的要求是让它为 GMT+05:30
【问题讨论】:
如果您需要特定格式的当前时区,一种方法是使用DateFormatter 以一种有些非正统的方式:
// formatter.timeZone is implicitly TimeZone.current
let formatter = DateFormatter()
formatter.dateFormat = "ZZZZ"
let abbreviation = formatter.string(from: Date())
这会返回一个格式为“GMT±xx:yy”的字符串;对我来说,这是“GMT-07:00”。
请参阅Date Field Symbol Table from TR35-31 了解更多信息。
【讨论】:
远离我的编译器进行测试,但这应该可以解决问题。
let tz = TimeZone.current.abbreviation()
if tz.contains(“+11”) || tz.contains(“+12”) {
// do nothing
} else {
tz.replaceAlOcurrences(of: “+”, with: “+0”)
}
【讨论】: