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.

EventView.swift
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

Gotchas

See also