Dates ยท SWIFT RECIPE

Add calendar days to a date

Account for calendar and daylight-saving changes.

main.swift
import Foundation
var calendar = Calendar(identifier: .gregorian)
calendar.timeZone = TimeZone(secondsFromGMT: 0)!
let start = Date(timeIntervalSince1970: 0)
if let tomorrow = calendar.date(byAdding: .day, value: 1, to: start) {
    print(Int(tomorrow.timeIntervalSince1970))
}

How it works

Calendar performs arithmetic in calendar units. A local day can have 23 or 25 hours around daylight-saving transitions, so fixed-second arithmetic can produce the wrong wall-clock time.

EXPECTED OUTPUT
86400

Keep in mind

Choose the calendar and time zone that fit your domain.

Read the Swift language reference