Why is date + 86,400 seconds not tomorrow?
Predict first
What hour does each date show in New York? (DST starts on 10 March 2024.)
var cal = Calendar(identifier: .gregorian)
cal.timeZone = TimeZone(identifier: "America/New_York")!
let start = cal.date(from: DateComponents(year: 2024, month: 3, day: 9, hour: 12))!
let plus24h = start.addingTimeInterval(24 * 3600)
let tomorrow = cal.date(byAdding: .day, value: 1, to: start)!
print(cal.component(.hour, from: plus24h), cal.component(.hour, from: tomorrow))
- 12 12
- 13 12
- 11 12
Predict first
How long is the 10 March 2024 day in New York, in seconds?
var cal = Calendar(identifier: .gregorian)
cal.timeZone = TimeZone(identifier: "America/New_York")!
let a = cal.date(from: DateComponents(year: 2024, month: 3, day: 10))!
let b = cal.date(from: DateComponents(year: 2024, month: 3, day: 11))!
print(cal.startOfDay(for: b).timeIntervalSince(cal.startOfDay(for: a)))
- 86400.0
- 82800.0
- 90000.0
Mental model. Adding 24 hours is walking exactly 100 metres. Adding a day is “same spot on the clock face tomorrow”, however long the day turned out to be.
A day is not always 24 hours. On a daylight-saving change a calendar day lasts 23 or 25 hours, and in rare zones a day can skip a whole stretch of local time. Adding 24 * 3600 seconds moves the instant by exactly that much, which can land at 1 pm instead of noon.
To mean “the same time tomorrow” ask the calendar: cal.date(byAdding: .day, value: 1, to:). Use startOfDay(for:) rather than building “00:00” by hand (midnight may not exist), isDate(_:inSameDayAs:) to compare days, and dateComponents([.day], from:to:) to count calendar days.
Clocks jumped forward overnight, so exactly 24 hours later reads 1 pm. byAdding: .day keeps the local time at noon.
The spring-forward day loses an hour: 23 × 3600 = 82,800 seconds.
Common traps
- Subtracting timestamps to count days. Across DST that gives 0.96 or 1.04 days. Count calendar days with
dateComponents. - Hand-building midnight. Some zones skip midnight on a DST change.
startOfDayhandles it.
They might follow up with
- Which day is shorter in the US spring? The DST-start day has 23 hours, so
startOfDayof the next day is 82,800 seconds later. - What about “8 am every day” reminders? Store the local time and zone, not a repeating interval.
Related questions
- 2.2.3 How do Calendar and DateComponents work, and why can YYYY give the wrong year?
- 2.2.5 How does Calendar handle “add a month”, “next Friday” and “start of the month”?
- 2.2.2 How do you handle time zones: what do you store, send and show?
← How do Calendar and DateComponents work, and why can YYYY give the… How does Calendar handle “add a month”, “next Friday” and “start of… →