import Foundation
let formatter = DateFormatter()
let displayLocalFormat = true or false
let timeZone = UTC
let dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
let currentDate = Date()
formatter.locale = displayLocalFormat ? Locale.current : Locale(identifier: "en_US_POSIX")
formatter.dateFormat = dateFormat
formatter.timeZone = timeZone
formatter.string(from: date) // This function returns date format 2024-05-23T11:16:24.706 a.m.Z
OK, but how are you testing it? What version of Xcode are you using to build your test? What platform are you running it on? And what version of that platform?
If you run my code in your test environment, what results do you see?
Finally, look at you code I see this:
let displayLocalFormat = true // or false
…
formatter.locale = displayLocalFormat ? Locale.current : Locale(identifier: "en_US_POSIX")
…
switch formatOrStyle {
…
case let .format(format):
formatter.dateFormat = format
}
This is wrong. If you’re using a fixed date format then you must use en_US_POSIX
. Anything else will not work the way you expect it to work.
See this post for a summary of the three valid ways to use a date formatter.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"