You can use the Date
and Calendar
classes provided by Swift to compare two dates in SwiftUI. Here's an example of an if
statement that checks if one date is earlier than another date:
import SwiftUI
struct ContentView: View {
let date1 = Date()
let date2 = Date(timeIntervalSinceNow: 3600) // One hour later than date1
var body: some View {
if date1 < date2 {
Text("Date 1 is earlier than Date 2")
} else {
Text("Date 2 is earlier than Date 1")
}
}
}
In this example, date1
and date2
are two Date
objects representing the current date/time and one hour later, respectively. The if
statement compares these two dates using the <
operator and displays a message accordingly.
You can use other comparison operators as well (e.g. >
, <=
, >=
) depending on your needs. Just make sure to use the appropriate operator for your desired comparison.
Asked: 2021-10-07 11:00:00 +0000
Seen: 10 times
Last updated: Sep 09 '22