Swift 3 - Comparing Date objects

后端 未结 14 1506
野的像风
野的像风 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:32

    Look this http://iswift.org/cookbook/compare-2-dates

    Get Dates:

    // Get current date
    let dateA = NSDate()
    
    // Get a later date (after a couple of milliseconds)
    let dateB = NSDate()
    

    Using SWITCH Statement

    // Compare them
    switch dateA.compare(dateB) {
        case .OrderedAscending     :   print("Date A is earlier than date B")
        case .OrderedDescending    :   print("Date A is later than date B")
        case .OrderedSame          :   print("The two dates are the same")
    }
    

    using IF Statement

     if dateA.compare(dateB) == .orderedAscending {
         datePickerTo.date = datePicker.date
     }
    
     //OR
    
     if case .orderedAcending = dateA.compare(dateB) {
    
     }
    
    0 讨论(0)
  • 2020-11-30 07:32

    For me the problem was that I had my own extension to Date class that was defining all the compare operators. Now (since swift 3) that Date is comparable, these extensions are not needed. So I commented them out and it worked.

    0 讨论(0)
  • 2020-11-30 07:34
       var strDateValidate = ""
                let dateFormatter = DateFormatter()
                dateFormatter.dateFormat = "yyyy-MM-dd"
    
                let firstDate = dateFormatter.date(from:lblStartTime.text!)
                let secondDate = dateFormatter.date(from:lblEndTime.text!)
    
                if firstDate?.compare(secondDate!) == .orderedSame || firstDate?.compare(secondDate!) == .orderedAscending {
                    print("Both dates are same or first is less than scecond")
                    strDateValidate = "yes"
                }
                else
                {
                    //second date is bigger than first
                    strDateValidate = "no"
                }
    
                if strDateValidate == "no"
                {
                    alertView(message: "Start date and end date for a booking must be equal or Start date must be smaller than the end date", controller: self)
                }
    
    0 讨论(0)
  • 2020-11-30 07:35

    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 { ... }
    
    0 讨论(0)
  • 2020-11-30 07:36

    As of the time of this writing, Swift natively supports comparing Dates with all comparison operators (i.e. <, <=, ==, >=, and >). You can also compare optional Dates but are limited to <, ==, and >. If you need to compare two optional dates using <= or >=, i.e.

    let date1: Date? = ...
    let date2: Date? = ...
    if date1 >= date2 { ... }
    

    You can overload the <= and >=operators to support optionals:

    func <= <T: Comparable>(lhs: T?, rhs: T?) -> Bool {
        return lhs == rhs || lhs < rhs
    }
    
    func >= <T: Comparable>(lhs: T?, rhs: T?) -> Bool {
        return lhs == rhs || lhs > rhs
    }
    
    0 讨论(0)
  • 2020-11-30 07:37

    SWIFT 3: Don't know if this is what you're looking for. But I compare a string to a current timestamp to see if my string is older that now.

    func checkTimeStamp(date: String!) -> Bool {
            let dateFormatter: DateFormatter = DateFormatter()
            dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
            dateFormatter.locale = Locale(identifier:"en_US_POSIX")
            let datecomponents = dateFormatter.date(from: date)
    
            let now = Date()
    
            if (datecomponents! >= now) {
                return true
            } else {
                return false
            }
        }
    

    To use it:

    if (checkTimeStamp(date:"2016-11-21 12:00:00") == false) {
        // Do something
    }
    
    0 讨论(0)
提交回复
热议问题