Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3 - Comparing Date objects

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:

enter image description here enter image description here

As you can see in both screenshots, these are two Date objects. But I get the following error: enter image description here

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?

like image 825
beeef Avatar asked Aug 18 '16 12:08

beeef


People also ask

How do I compare two dates in Swift?

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.

How do you compare Date objects?

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.

How do I get todays date in Swift?

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.


2 Answers

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 { ... } 
like image 81
Ankit Thakur Avatar answered Oct 08 '22 19:10

Ankit Thakur


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.

Comparison function

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: ",  ") } 

Sample Use

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 
like image 25
Mobile Dan Avatar answered Oct 08 '22 19:10

Mobile Dan