Is it possible to use the DateFormatter.localizedString(from:dateStyle:timeStyle:)
method to return a localized string for a given Date
, but only for a specific timezone? My app receives an ISO 8601-ish date string (such as 2021-08-27T11:00:00.0000000-04:00
), and I need to display it in the user’s local device language, but it has to always be in the New York timezone, no matter where they are in the world. If I specify my own format, I can create my own DateFormatter and set a timezone:
let outputFormatter = DateFormatter()
outputFormatter.timeZone = TimeZone(identifier: "America/New_York")
outputFormatter.dateFormat = "MMMM d, yyyy – h:mm a"
ticketDateString = outputFormatter.string(from: date)
But this method forces the user to see AM/PM even if they have 24-hour time turned on, and it always shows the month before the day, even if their device/locale uses a different format. I would much rather use DateFormatter.localizedString(from:dateStyle:timeStyle:)
is a static method, so I can’t call it on an instance of my own DateFormatter with a custom timezone.