Format dates the reader's way with Date.FormatStyle
Ask for the date fields you want and let the locale decide the order, separators and 12 or 24-hour clock. No more dateFormat strings.
iOS 15+macOS 12+ Swift 5.9
A DateFormatter with dateFormat = "MM/dd/yyyy" shows 11/18/2026 to everyone, which reads as nonsense outside the US. Date.FormatStyle turns it around: you say which fields you want, and the reader’s locale and settings decide how they are written.
import SwiftUI
struct EventView: View {
let start = Date.now
var body: some View {
VStack(alignment: .leading, spacing: 8) {
// "18 November 2026" in the UK, "November 18, 2026" in the US
Text(start, format: .dateTime.day().month(.wide).year())
// "Wed 18 Nov" in the UK, "Wed, Nov 18" in the US
Text(start, format: .dateTime.weekday(.abbreviated).day().month(.abbreviated))
// "15:30" or "3:30 PM", following the reader's clock setting
Text(start, format: .dateTime.hour().minute())
// Quick presets for date and time together
Text(start.formatted(date: .abbreviated, time: .shortened))
}
}
}
#Preview("United Kingdom") {
EventView()
.environment(\.locale, Locale(identifier: "en_GB"))
}
#Preview("United States") {
EventView()
.environment(\.locale, Locale(identifier: "en_US"))
}How it works
.dateTimestarts with no fields. Each call such as.day(),.month(.wide)or.hour()adds one, and the order of the calls does not matter: the locale puts them in the right order with the right punctuation..hour()follows the reader’s 12 or 24-hour preference from Settings, so never add “a” or “HH” yourself.formatted(date:time:)has presets (.numeric,.abbreviated,.long,.complete, and.omittedto leave one half out) for when you do not need to pick fields.- In SwiftUI,
Text(_:format:)uses the environment’s locale and calendar, which makes previews in other regions a one-line change.
Gotchas
- For dates you send to a server or store as text, use a fixed format, not a localised one:
start.formatted(.iso8601)gives “2026-11-18T15:30:00Z”. - If you must keep a
DateFormatter, usesetLocalizedDateFormatFromTemplate("dMMMMy")instead ofdateFormat. It picks the right order for the locale the same way. Date.FormatStyleuses the time zone of the device unless you set one with.timeZone(...)on the style. A date shown “wrong” by an hour is usually a time zone question, not a formatting one.