I'm updating my app to Swift 3.0 syntax (I know it's still in beta but I want to be prepared as soon as it released).
Until the previous Beta of Xcode (Beta 5) I was able to compare two Date objects using the operands <, > and ==. But in the latest beta (Beta 6) this isn't working any more. Here are some screenshots:

As you can see in both screenshots, these are two Date objects. But I get the following error: 
What am I doing wrong? The functions are still declared in the Date class:
static func >(Date, Date)Returns true if the left hand Date is later in time than the right hand Date.
Is this just a Beta bug or am I doing something wrong?
Here we will create two different date objects using DateFormatter. Then use compare function of Date class to check whether our two different date objects are same,one date is greater than other date or one date is smaller than other date in swift.
For comparing the two dates, we have used the compareTo() method. If both dates are equal it prints Both dates are equal. If date1 is greater than date2, it prints Date 1 comes after Date 2. If date1 is smaller than date2, it prints Date 1 comes after Date 2.
Get current time in “YYYY-MM–DD HH:MM:SS +TIMEZONE” format in Swift. This is the easiest way to show the current date-time.
I have tried this snippet (in Xcode 8 Beta 6), and it is working fine.
let date1 = Date() let date2 = Date().addingTimeInterval(100) if date1 == date2 { ... } else if date1 > date2 { ... } else if date1 < date2 { ... }
Date is Comparable & Equatable (as of Swift 3)This answer complements @Ankit Thakur's answer.
Since Swift 3 the Date struct (based on the underlying NSDate class) adopts the Comparable and Equatable protocols.
Comparable requires that Date implement the operators: <, <=, >, >=.Equatable requires that Date implement the == operator.Equatable allows Date to use the default implementation of the != operator (which is the inverse of the Equatable == operator implementation).The following sample code exercises these comparison operators and confirms which comparisons are true with print statements.
import Foundation func describeComparison(date1: Date, date2: Date) -> String { var descriptionArray: [String] = [] if date1 < date2 { descriptionArray.append("date1 < date2") } if date1 <= date2 { descriptionArray.append("date1 <= date2") } if date1 > date2 { descriptionArray.append("date1 > date2") } if date1 >= date2 { descriptionArray.append("date1 >= date2") } if date1 == date2 { descriptionArray.append("date1 == date2") } if date1 != date2 { descriptionArray.append("date1 != date2") } return descriptionArray.joined(separator: ", ") } let now = Date() describeComparison(date1: now, date2: now.addingTimeInterval(1)) // date1 < date2, date1 <= date2, date1 != date2 describeComparison(date1: now, date2: now.addingTimeInterval(-1)) // date1 > date2, date1 >= date2, date1 != date2 describeComparison(date1: now, date2: now) // date1 <= date2, date1 >= date2, date1 == date2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With