【发布时间】:2017-04-19 03:38:37
【问题描述】:
当尝试将“2016-06-23 12:00:00”转换为 UTC 日期时,我得到“2016-06-23 10:00:00”
第一个日期是 GMT+1,我想将其转换为 UTC。如果我没记错 GMT+0 == UTC 那么 12:00 应该是 11:00 对吧?但我总是得到 10:00。为什么会这样?如何正确转换?
我在操场上和实际设备上都试过这个
这是我使用的代码:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let datestring:String = "2016-06-23 12:00:00"
print("1: "+datestring)
print("2: "+convertDateToUTC(datestring))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func convertDateToUTC(_ datestring:String) -> String {
let dateForm = DateFormatter()
dateForm.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateForm.timeZone = TimeZone(abbreviation: "GMT+1")
print(TimeZone.current.abbreviation()!)
let date = dateForm.date(from: datestring)
dateForm.timeZone = TimeZone(abbreviation: "UTC")
let date1 = dateForm.string(from: date!)
return date1
}
}
输出:
1: 2016-06-23 12:00:00
GMT+1
2: 2016-06-23 10:00:00
【问题讨论】: