// When 24-hour time is set in user settings on Apple platforms
// (Settings > General > Date & Time > 24 Hour Time)
// 13:43 in afternoon ET, for demonstration
let exampleDateAfternoon = Date(timeIntervalSince1970: 1770230613)
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(abbreviation: "EST") // so this example is reproducible
// With this dateFormat, we expect to print:
// 2026-02-04 01:43:33 PM -0500
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss a Z"
print(dateFormatter.string(from: exampleDateAfternoon))
// Unexpected: user's preference overrides "hh" - prints (note that 13 PM is wrong):
// 2026-02-04 13:43:33 PM -0500
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
print(dateFormatter.string(from: exampleDateAfternoon))
// Fix: Explicitly setting a locale restores the expected behaviour - prints:
// 2026-02-04 01:43:33 PM -0500
let utcDateFormat: ISO8601DateFormatter = ISO8601DateFormatter()
print(utcDateFormat.string(from: exampleDateAfternoon))
// Alternative: Only use if you are okay with the ISO-8601 format - prints:
// 2026-02-04T18:43:33Z