iOS
date comparison
Swift
mobile development
date manipulation

iOS Compare two dates

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Comparing two dates on iOS is easy if you know what "compare" really means in your feature. Sometimes you want exact absolute time ordering, sometimes you want "same calendar day," and sometimes you only care about one component such as month or hour. Using the right comparison API matters more than writing a one-line operator.

Exact Time Comparison with Date

In Swift, Date represents an absolute moment in time. For exact ordering, direct comparison operators are usually enough.

swift
1import Foundation
2
3let now = Date()
4let later = now.addingTimeInterval(60)
5
6print(now < later)   // true
7print(now == later)  // false
8print(now > later)   // false

This is the correct approach when the question is:

  • did event A happen before event B
  • is this timestamp expired
  • which of these moments is earlier

At this level, time zones are not part of the comparison because Date is already an absolute instant.

Using compare(_:)

Foundation also offers the older comparison method:

swift
1let result = now.compare(later)
2
3switch result {
4case .orderedAscending:
5    print("now is earlier")
6case .orderedDescending:
7    print("now is later")
8case .orderedSame:
9    print("same instant")
10}

This is functionally similar to direct comparison. Use whichever form is clearer in the surrounding code.

Compare Calendar Days, Not Instants

Many bugs come from using exact Date comparison when the product requirement is actually "same day in the user's calendar."

For that, use Calendar.

swift
1import Foundation
2
3let calendar = Calendar.current
4
5let d1 = Date()
6let d2 = d1.addingTimeInterval(3600)
7
8print(calendar.isDate(d1, inSameDayAs: d2))

This answers a different question from d1 == d2. Two times can be on the same day without being the same instant.

That distinction is critical for:

  • reminders
  • booking UIs
  • grouped timelines
  • "today" or "yesterday" labels

Compare Specific Components

If you need to compare by month, year, or another component, extract the parts with Calendar.

swift
1let c1 = calendar.dateComponents([.year, .month, .day], from: d1)
2let c2 = calendar.dateComponents([.year, .month, .day], from: d2)
3
4print(c1.year == c2.year)
5print(c1.month == c2.month)

Or compare at a chosen granularity:

swift
let ordered = calendar.compare(d1, to: d2, toGranularity: .day)
print(ordered == .orderedSame)

This is usually better than manually comparing several date components one by one.

Parsing Before Comparing

If your dates start as strings, parse them first and make the formatter deterministic.

swift
1import Foundation
2
3let formatter = ISO8601DateFormatter()
4
5let start = formatter.date(from: "2026-03-07T10:00:00Z")!
6let end = formatter.date(from: "2026-03-07T12:00:00Z")!
7
8print(start < end)

Do not compare date strings lexically unless you are certain the format guarantees chronological lexical order and consistent time-zone handling.

Time Zones and User Expectations

Date values are absolute, but presentation and calendar-based comparisons depend on time zone. The same instant can fall on different local calendar days in different zones.

So if the app says:

  • "same day"
  • "today"
  • "this month"

then Calendar and the selected time zone are part of the logic, not just the UI.

This is where many subtle date bugs originate.

Common Pitfalls

  • Using exact Date equality when the business rule is really "same day."
  • Comparing string representations instead of parsed Date values.
  • Ignoring calendar and time-zone context for user-facing comparisons.
  • Manually comparing components when Calendar already supports granularity-aware comparison.
  • Assuming two dates shown with the same local date label are equal instants.

Summary

  • Use direct Date comparison for absolute time ordering.
  • Use Calendar for same-day or component-based logic.
  • Parse strings into Date objects before comparing.
  • Remember that calendar comparisons depend on time zone and user context.
  • Pick the comparison method based on the business meaning, not just the data type.

Course illustration
Course illustration

All Rights Reserved.