0

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.

Eugene
  • 3,417
  • 5
  • 25
  • 49
  • See the duplicate; Use `jj` for your hour when creating your time template – Paulw11 Aug 04 '21 at 21:54
  • When displaying a date to the user you should always use date and time style instead of using a fixed date format. This will respect the user's device locale and settings. You can use the current or fixed timezone/locale. Check this [post](https://stackoverflow.com/a/28347285/2303865) which gives you all the options you might need when displaying a date to the user. If you need to parse your iso8601 date string before displaying it you can check this [post](https://stackoverflow.com/a/28016692/2303865) – Leo Dabus Aug 05 '21 at 00:59

0 Answers0