how to convert date UTC to local and Local to UTC?

Viewed 761

I want to convert date from local to UTC and UTC to Local.

For eg. Apr 1, 2015, 12:00 PM is UTC then

Apr 1, 2015, 5:30 PM is Local for me but I am getting reverse of it

let dateFormatter2 = DateFormatter()
dateFormatter2.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"[![enter image description here][1]][1]
dateFormatter2.timeZone = NSTimeZone.local
let date2 = dateFormatter2.date(from: "2015-04-01T12:00:00")


dateFormatter2.timeZone = NSTimeZone(name: "UTC")! as TimeZone
let date3 = dateFormatter2.date(from: "2015-04-01T12:00:00")

I am getting Apr 1, 2015, 5:30 PM as UTC and Apr 1, 2015, 12:00 PM as Local

enter image description here

3 Answers
Related