I working on an app that receives events from the backend. The dates are returned in UTC+0 timezone. The app has to show the starting time in the device time zone. So I have developed a function to recalculate the date.
So far I am in UK and I have changed the device time zone to be in Spain. The code I have written is:
static func reCalculateDeviceDate(from date: Date?) -> Date? {
guard let date = date else { return nil }
print("Input date: ", date)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
dateFormatter.timeZone = .current
dateFormatter.locale = Locale.current
let dateString = dateFormatter.string(from: date)
let outputDate = dateFormatter.date(from: dateString)
print("Recalculate date in string format: ", dateString)
print("Output Date: ", outputDate!)
return dateFormatter.date(from: dateString)
}
The output:
Input date: 2020-02-10 15:47:43 +0000
Recalculated date in string format: 2020-02-10 16:47:43
Output Date: 2020-02-10 15:47:43 +0000
Despite dateString shows the recalculated date, the outputDate it's wrongly created.
What am I missing?