Swift 3.0 : Convert server UTC time to local time and vice-versa
I don't know what's wrong with your code.
But looks too much unnecessary things are there like you're setting calendar, fetching some elements from string.Here is my small version of UTCToLocal and localToUTC function.
But for that you need to pass string in specific format. Cause I've forcly unwrapped date objects. But you can use some guard conditions to prevent crashing your app.
func localToUTC(dateStr: String) -> String? { let dateFormatter = DateFormatter() dateFormatter.dateFormat = "h:mm a" dateFormatter.calendar = Calendar.current dateFormatter.timeZone = TimeZone.current if let date = dateFormatter.date(from: dateStr) { dateFormatter.timeZone = TimeZone(abbreviation: "UTC") dateFormatter.dateFormat = "H:mm:ss" return dateFormatter.string(from: date) } return nil}func utcToLocal(dateStr: String) -> String? { let dateFormatter = DateFormatter() dateFormatter.dateFormat = "H:mm:ss" dateFormatter.timeZone = TimeZone(abbreviation: "UTC") if let date = dateFormatter.date(from: dateStr) { dateFormatter.timeZone = TimeZone.current dateFormatter.dateFormat = "h:mm a" return dateFormatter.string(from: date) } return nil}
and call these function like below.
print(utcToLocal(dateStr: "13:07:00"))print(localToUTC(dateStr: "06:40 PM"))
Hope this will help you.
Happy coding!!
Mrugesh's answer is perfect, but if someone need to use their own formats, or in some different format, I've generalised it so you can give different format or same in both parameters.
func localToUTC(date:String, fromFormat: String, toFormat: String) -> String { let dateFormatter = DateFormatter() dateFormatter.dateFormat = fromFormat dateFormatter.calendar = NSCalendar.current dateFormatter.timeZone = TimeZone.current dateFormatter.date let dt = dateFormatter.date(from: date) dateFormatter.timeZone = TimeZone(abbreviation: "UTC") dateFormatter.dateFormat = toFormat return dateFormatter.string(from: dt!)}func UTCToLocal(date:String, fromFormat: String, toFormat: String) -> String { let dateFormatter = DateFormatter() dateFormatter.dateFormat = fromFormat dateFormatter.timeZone = TimeZone(abbreviation: "UTC") let dt = dateFormatter.date(from: date) dateFormatter.timeZone = TimeZone.current dateFormatter.dateFormat = toFormatreturn dateFormatter.string(from: dt!)}let localDateAsString = UTCToLocal(date: dateAsString!, fromFormat: "hh:mm a, dd MMM yyyy", toFormat: "hh:mm a, dd MMM yyyy")
You can use it as above. Hope it helps.
By the help of Mrugesh Tank Answer,
I have updated his answer and creating the extensions for the date. So that you can easily access the functions from anywhere either from ViewController or either from cell class as well.
extension String { //MARK:- Convert UTC To Local Date by passing date formats value func UTCToLocal(incomingFormat: String, outGoingFormat: String) -> String { let dateFormatter = DateFormatter() dateFormatter.dateFormat = incomingFormat dateFormatter.timeZone = TimeZone(abbreviation: "UTC") let dt = dateFormatter.date(from: self) dateFormatter.timeZone = TimeZone.current dateFormatter.dateFormat = outGoingFormat return dateFormatter.string(from: dt ?? Date()) } //MARK:- Convert Local To UTC Date by passing date formats value func localToUTC(incomingFormat: String, outGoingFormat: String) -> String { let dateFormatter = DateFormatter() dateFormatter.dateFormat = incomingFormat dateFormatter.calendar = NSCalendar.current dateFormatter.timeZone = TimeZone.current let dt = dateFormatter.date(from: self) dateFormatter.timeZone = TimeZone(abbreviation: "UTC") dateFormatter.dateFormat = outGoingFormat return dateFormatter.string(from: dt ?? Date()) }}
Example how to use it:-
Note:- eventStartDate is the string which you have to converted in your format like this:- "2018-07-11T16:22:00.000Z" let finalDate = eventStartDate.UTCToLocal(incomingFormat: "yyyy-MM-dd'T'HH:mm:ss.SSSZ", outGoingFormat: "MMM d, yyyy h:mm a")