Swift DateTimeFormatter TimeZone

120 Views Asked by At

I'm trying to convert a date-time string to a date, with a DateFormatter with the TimeZone set to UTC.

The input string is in the format yyyy-MM-dd'T'HH:mm:ss'Z'.

For some reason, the subsequent Date produced is in UTC - 1:00:00. For example a date-time string of "2023-04-28T12:18:15Z" is producing a Date of 2023-04-28 11:18:15 UTC.

    let inputLogDateFormatter: DateFormatter = DateFormatter()
    private init() {
        self.inputLogDateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
        self.inputDateFormatter.timeZone = TimeZone(abbreviation: "UTC")
        self.inputDateFormatter.locale = Locale(identifier: "en_US_POSIX")
    }
......
let logDateTime: String = // LogString in 'inputLogDate' format.
let date: Date = self.inputLogDateFormatter.date(from: logDateTime) ?? Date(timeIntervalSince1970: 0) // Produces a time - 1 hour from input time string.
0

There are 0 best solutions below