Порівняння двох дат

Swift:
Порівняння двох дат

How to:

“Як це зробити:”

Swift’s Date objects can be compared using comparison operators. Here’s how to do it:

import Foundation

let now = Date()
let tomorrow = Calendar.current.date(byAdding: .day, value: 1, to: now)!

// Check if one date comes before the other
if now < tomorrow {
    print("Now is earlier than tomorrow.")
} else {
    print("Now is not earlier than tomorrow.")
}

// Check if two dates are the same moment
if now == tomorrow {
    print("Now is the same as tomorrow.")
} else {
    print("Now is not the same as tomorrow.")
}

// Comparing dates for sorting
let dates = [tomorrow, now]
let sortedDates = dates.sorted(by: { $0 < $1 })
print("Sorted dates: \(sortedDates)")

Sample output:

Now is earlier than tomorrow.
Now is not the same as tomorrow.
Sorted dates: [current date's timestamp, tomorrow's date's timestamp]

Deep Dive

“Детальний огляд”

In the old times of Objective-C, we compared dates using NSDate and methods like compare:. Now, with Swift, comparison operators make the code cleaner.

Alternatives to simple comparison include using Calendar and DateComponents for more complex operations, like comparing only specific components (just the day, month, or year).

For implementation, Swift uses TimeInterval (a Double representing seconds) to measure exact differences between two Date objects.

See Also

“Дивіться також”