简体   繁体   English

Swift 3 - 比较日期对象

[英]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).我正在将我的应用程序更新为 Swift 3.0 语法(我知道它仍处于测试阶段,但我想在它发布后立即做好准备)。

Until the previous Beta of Xcode (Beta 5) I was able to compare two Date objects using the operands < , > and == .直到之前的 Xcode 测试版(测试版 5),我才能使用操作数<>==比较两个Date对象。 But in the latest beta (Beta 6) this isn't working any more.但是在最新的测试版(Beta 6)中,这不再起作用了。 Here are some screenshots:以下是一些截图:

在此处输入图片说明 在此处输入图片说明

As you can see in both screenshots, these are two Date objects.正如您在两个屏幕截图中看到的,这是两个Date对象。 But I get the following error:但我收到以下错误: 在此处输入图片说明

What am I doing wrong?我做错了什么? The functions are still declared in the Date class:这些函数仍然在Date类中声明:

static func >(Date, Date)

Returns true if the left hand Date is later in time than the right hand Date.如果左侧 Date 在时间上晚于右侧 Date,则返回 true。

Is this just a Beta bug or am I doing something wrong?这只是一个 Beta 错误还是我做错了什么?

I have tried this snippet (in Xcode 8 Beta 6), and it is working fine.我已经尝试过这个片段(在 Xcode 8 Beta 6 中),它运行良好。

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) DateComparableEquatable (从 Swift 3 开始)

This answer complements @Ankit Thakur's answer.这个答案补充了@Ankit Thakur 的答案。

Since Swift 3 the Date struct (based on the underlying NSDate class) adopts the Comparable and Equatable protocols.从 Swift 3 开始, Date结构(基于底层NSDate类)采用ComparableEquatable协议。

  • Comparable requires that Date implement the operators: < , <= , > , >= . Comparable要求Date实现运算符: <<=>>=
  • Equatable requires that Date implement the == operator. Equatable要求Date实现==运算符。
  • Equatable allows Date to use the default implementation of the != operator (which is the inverse of the Equatable == operator implementation). Equatable允许Date使用!=运算符的默认实现(与Equatable ==运算符实现相反)。

The following sample code exercises these comparison operators and confirms which comparisons are true with print statements.下面的示例代码练习这些比较运算符,并使用print语句确认哪些比较是正确的。

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

from Swift 3 and above, Date is Comparable so we can directly compare dates likeSwift 3 及以上版本开始,Date is Comparable所以我们可以直接比较日期,例如

let date1 = Date()
let date2 = Date().addingTimeInterval(50)

let isGreater = date1 > date2
print(isGreater)

let isSmaller = date1 < date2
print(isSmaller)

let isEqual = date1 == date2
print(isEqual)

Alternatively We can create extension on Date或者我们可以在日期上创建扩展

extension Date {

  func isEqualTo(_ date: Date) -> Bool {
    return self == date
  }
  
  func isGreaterThan(_ date: Date) -> Bool {
     return self > date
  }
  
  func isSmallerThan(_ date: Date) -> Bool {
     return self < date
  }
}

Use: let isEqual = date1.isEqualTo(date2)使用: let isEqual = date1.isEqualTo(date2)

Don't use comparators < , > , == , != .不要使用比较器<>==!=
Use compare(_ other: Date) function.使用compare(_ other: Date)函数。

Usage用法

// Get current date
let dateA = Date()

// Get a later date (after a couple of milliseconds)
let dateB = Date()

// Compare them using switch
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")
}

// Compare them using if
if dateA.compare(dateB) == .orderedAscending {
    datePickerTo.date = datePicker.date
}

For me the problem was that I had my own extension to Date class that was defining all the compare operators.对我来说,问题是我有自己的 Date 类扩展,它定义了所有的比较运算符。 Now (since swift 3) that Date is comparable, these extensions are not needed.现在(从 swift 3 开始)日期是可比的,不需要这些扩展。 So I commented them out and it worked.所以我把它们注释掉了,它奏效了。

SWIFT 3: Don't know if this is what you're looking for. SWIFT 3:不知道这是否是您要找的。 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
}

If you want to ignore seconds for example you can use例如,如果您想忽略秒,则可以使用

func isDate(Date, equalTo: Date, toUnitGranularity: NSCalendar.Unit) -> Bool 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)

To compare date only with year - month - day and without time for me worked like this:仅将日期与年 - 月 - 日进行比较并且没有时间对我来说是这样工作的:

     let order = Calendar.current.compare(self.startDate, to: compareDate!, toGranularity: .day)  

                      switch order {
                        case .orderedAscending:
                            print("\(gpsDate) is after \(self.startDate)")
                        case .orderedDescending:
                            print("\(gpsDate) is before \(self.startDate)")
                        default:
                            print("\(gpsDate) is the same as \(self.startDate)")
                        }

As of the time of this writing, Swift natively supports comparing Dates with all comparison operators (ie < , <= , == , >= , and > ).在撰写本文时,Swift 原生支持将日期与所有比较运算符(即<<===>=> )进行比较。 You can also compare optional Dates but are limited to < , == , and > .您还可以比较可选日期,但仅限于<==> If you need to compare two optional dates using <= or >= , ie如果您需要使用<=>=比较两个可选日期,即

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
}
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) }

Another way to do it:另一种方法:

    switch date1.compare(date2) {
        case .orderedAscending:
            break

        case .orderedDescending:
            break;

        case .orderedSame:
            break
    }
   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)
            }

Swift 5:斯威夫特 5:

1) If you use Date type: 1)如果您使用日期类型:

let firstDate  = Date()
let secondDate = Date()

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

2) If you use String type: 2) 如果使用 String 类型:

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%.我不确定或第二个选项 100% 有效。 But how much would I not change the values of firstStringDate and secondStringDate the result was correct.但是我不会改变 firstStringDate 和 secondStringDate 的值多少结果是正确的。

in Swift 3,4 you should use "Compare".在 Swift 3,4 中,您应该使用“比较”。 for example:例如:

DateArray.sort { (($0)?.compare($1))! == .orderedDescending }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM