When trying to convert "2016-06-23 12:00:00" to a UTC Date I get "2016-06-23 10:00:00"
The first Date is in GMT+1 which I want to convert to UTC. If I'm not mistaken GMT+0 == UTC so 12:00 should be 11:00 right? But I always get 10:00. Why is that the case and how do I convert it correctly?
I both tried this in the playground and on an actual device
This is the Code I used:
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
}
}
output:
1: 2016-06-23 12:00:00
GMT+1
2: 2016-06-23 10:00:00