Swift:
Comparing two dates

How to:

Swift uses the Date type for date and time. Here’s a simple take on comparing two dates:

import Foundation

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy/MM/dd HH:mm"

// Creating two date objects
let date1 = dateFormatter.date(from: "2023/01/01 09:00")!
let date2 = dateFormatter.date(from: "2023/02/01 10:00")!

// Comparing dates
if date1 == date2 {
    print("Dates are the same")
} else if date1 < date2 {
    print("Date1 is earlier than Date2")
} else {
    print("Date1 is later than Date2")
}

Sample output:

Date1 is earlier than Date2

Comparison operators can be used because Date conforms to the Comparable protocol.

Deep Dive:

Dates didn’t always come in handy objects. Originally, you had to wrangle individual components like year, month, and day. Much worse. Now, Date objects in Swift handle heavy lifting, and comparing them is straightforward with built-in operators.

Before Swift and Cocoa’s Date, Objective-C used NSDate, but they’re bridgeable, so old code can still play nice.

And hey, not just <, >, and == — you can also use timeIntervalSince(_:) for more granular control, like:

let timeInterval = date2.timeIntervalSince(date1)

This gives you the difference in seconds. Positive value: date2 is ahead; negative: it’s behind; zero: they’re identical. Super useful for timers, countdowns, and tracking durations. Under the hood, dates are just reference points in time—think of them as fancy timestamps.

See Also: