I have a datafile with a date in STRING format, and I need to convert it to a DATE object
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX") // set locale to reliable US_POSIX dateFormatter.dateFormat = "MM-dd-yyyy HH:mm a"
let date = dateFormatter.date(from:v[2])!
let timeZoneOffset = Double(TimeZone.current.secondsFromGMT(for: date))
let localDate = Calendar.current.date(byAdding: .second, value: Int(timeZoneOffset), to: date)!
print("Input: \(v[2]) Date: \(date) Local: \(localDate) = \(timeZoneOffset)")
here is what I get
Input: 08-22-2024 11:51 AM
Date: 2024-08-22 07:51:00 +0000
Local: 2024-08-22 00:51:00 +0000
TZ : -25200.0 [7 hours]
the first conversion is off by 4 hours, the seond is off by 11 hours
even though the timezone offset is only 7 hours
I need the date object to reflect EXACTLY what the string is, regardless of TZ