I’m using SwiftUI’s Text(_:style:) with the .relative style to show how long ago a date occurred. According to the docs:
A style displaying a date as relative to now.
I expected it to show the precise difference between a past date and the current date. However, I noticed that two dates that are 3 days apart both display the same relative string under certain conditions.
Code snippet to reproduce- (using GMT time zone and the system calendar) IMPORTANT: To reproduce this, set your Mac’s system clock to 8 September 2025, 3:00 AM. SwiftUI’s relative style uses the current system time as its reference point, so changing the clock is necessary to see the behavior.
Settings
- Mac is set to Central European Time zone (but this behaviour was also reproduced by one of my app's users in the US.)
- Mac OS Sequoia 15.5
- XCode 16.4
- tested on an iOS Simulator and a real iPhone both running iOS 18.5
struct TestDateView: View {
var body: some View {
// 8. July 10AM to 8. September 3AM = Shows 2 months 2 days
let startDate1: Date = Calendar.current.date(from: .init(calendar: .current, timeZone: .gmt,
year: 2025, month: 7, day: 8, hour: 10, minute: 0, second: 0))!
// 5. July 10AM to 8. September 3AM = Shows 2 months 2 days
let startDate2: Date = Calendar.current.date(from: .init(calendar: .current, timeZone: .gmt,
year: 2025, month: 7, day: 5, hour: 10, minute: 0, second: 0))!
// IMPORTANT!: Need to set MAC's clock to 8. September 3:00 AM to reproduce this bug
VStack {
Text(startDate1, style: .relative)
Text(startDate2, style: .relative)
}
}
}
- How exactly does the .relative style work internally?
- Is it expected that different dates can collapse into the same result like this, or is there a better way to use .relative to get more precise results?
PS: I know about DateComponents and DateFormatter for exact calculations, but I’d like to understand this approach since it auto-updates natively with no timers or publishers.