Swift 3 - Comparing Date objects

后端 未结 14 1507
野的像风
野的像风 2020-11-30 07:14

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 wa

相关标签:
14条回答
  • 2020-11-30 07:38

    Swift 5:

    1) If you use Date type:

    let firstDate  = Date()
    let secondDate = Date()
    
    print(firstDate > secondDate)  
    print(firstDate < secondDate)
    print(firstDate == secondDate)
    

    2) If you use String type:

    let firstStringDate  = "2019-05-22T09:56:00.1111111"
    let secondStringDate = "2019-05-22T09:56:00.2222222"
    
    print(firstStringDate > secondStringDate)  // false
    print(firstStringDate < secondStringDate)  // true
    print(firstStringDate == secondStringDate) // false 
    

    I'm not sure or the second option works at 100%. But how much would I not change the values of firstStringDate and secondStringDate the result was correct.

    0 讨论(0)
  • 2020-11-30 07:39

    Another way to do it:

        switch date1.compare(date2) {
            case .orderedAscending:
                break
    
            case .orderedDescending:
                break;
    
            case .orderedSame:
                break
        }
    
    0 讨论(0)
  • 2020-11-30 07:41
    extension Date {
     func isBetween(_ date1: Date, and date2: Date) -> Bool {
        return (min(date1, date2) ... max(date1, date2)).contains(self)
      }
    }
    
    let resultArray = dateArray.filter { $0.dateObj!.isBetween(startDate, and: endDate) }
    
    0 讨论(0)
  • 2020-11-30 07:45

    If you want to ignore seconds for example you can use

    func isDate(Date, equalTo: Date, toUnitGranularity: NSCalendar.Unit) -> Bool

    Example compare if it's the same day:

    Calendar.current.isDate(date1, equalTo: date2, toGranularity: .day)
    
    0 讨论(0)
  • 2020-11-30 07:51

    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
    
    0 讨论(0)
  • 2020-11-30 07:51

    in Swift 3,4 you should use "Compare". for example:

    DateArray.sort { (($0)?.compare($1))! == .orderedDescending }
    
    0 讨论(0)
提交回复
热议问题