Swift:检查2个NSDates是否相同

时间:2015-04-01 13:55:58

标签: ios swift nsdate

非常简单,我确信这是一个快速的错误。我试图检查2个NSDates是否相同。即使打印输出位于

之下,此声明也从未通过
println("\(statistics.startDate) \(healthObject.date)")

if healthObject.date.isEqualToDate(statistics.startDate) 
{
      healthObject.flights =  Int(quantity.doubleValueForUnit(HKUnit.countUnit()))
      println(Int(quantity.doubleValueForUnit(HKUnit.countUnit())))
}

2015-03-30 13:56:50 +0000 2015-03-30 13:56:50 +0000

2015-03-30 13:56:50 +0000 2015-03-31 13:56:50 +0000

2015-03-31 13:56:50 +0000 2015-03-30 13:56:50 +0000

2015-03-31 13:56:50 +0000 2015-03-31 13:56:50 +0000

解决方案 正如一位回答的精彩人士指出的那样,日期可能是相同的,直到第二级。奇怪的是,这些值来自 HealthKit

在iOS8中对我有用的是:

   if let dif = calender?.compareDate(statistics.startDate, toDate: healthObject.date, toUnitGranularity: NSCalendarUnit.SecondCalendarUnit)
   {
      println(Int(quantity.doubleValueForUnit(HKUnit.countUnit())))
      println("\(statistics.startDate) \(healthObject.date)")
      healthObject.flights =  Int(quantity.doubleValueForUnit(HKUnit.countUnit()))

   }

4 个答案:

答案 0 :(得分:4)

检查日期是否相等的最简单方法是使用NSDate.isEqualToDate()方法。

NSCalendar.currentCalendar().compareDate(date1, toDate: date2, toUnitGranularity: NSCalendarUnit.CalendarUnitSecond)

如果您使用了相当多的日期算术检查this库。

答案 1 :(得分:1)

您可以使用此扩展程序:

extension NSDate
{
    func isGreaterThanDate(dateToCompare : NSDate) -> Bool
    {
        if self.compare(dateToCompare) == NSComparisonResult.OrderedDescending
        {
            return true
        }

        return false
    }


    func isLessThanDate(dateToCompare : NSDate) -> Bool
    {
        if self.compare(dateToCompare) == NSComparisonResult.OrderedAscending
        {
            return true
        }

        return false
    }


    func isEqualToDate(dateToCompare : NSDate) -> Bool
    {    
        if self.compare(dateToCompare) == NSComparisonResult.OrderedSame
        {
            return true
        }

        return false
    }
}

扩展程序可以正常检查:

var date1 : NSDate = NSDate()
var date2 : NSDate = date1.dateByAddingTimeInterval(-100)

print("\(date1)-----\(date2)\n")


if date1.isEqualToDate(date2) {
   print("date1 and date 2 are the same")

}else if date1.isLessThanDate(date2) {
   print("date 1 is less than date2")

 }else if date1.isGreaterThanDate(date2){
   print("date1 is more than date 2")
  }

只需更改您添加或减去的值。也许你必须使用dateformatter作为你的日期

答案 2 :(得分:1)

您应该能够将NSDates与==运算符(xCode 6.2)进行比较。我尝试了以下简单的比较

let startDate = NSDate()
var dateComponents = NSDateComponents()           
var calendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)
var endDate = calendar!.dateByAddingComponents(dateComponents, toDate: startDate, options: nil)

if(startDate == endDate) // true here
{
    println("equal")
}

如果我更改 endDate

dateComponents.second = 5
endDate = calendar!.dateByAddingComponents(dateComponents, toDate: startDate, options: nil)

if(startDate == endDate) // false here
{
    println("equal")
}

答案 3 :(得分:0)

我实际上找到了解决问题的新方法

let calender = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
if let dif = calender?.compareDate(statistics.startDate, toDate: healthObject.date, toUnitGranularity: NSCalendarUnit.HourCalendarUnit)
     {
         println(Int(quantity.doubleValueForUnit(HKUnit.countUnit())))
         println("\(statistics.startDate) \(healthObject.date)")
         healthObject.flights =  Int(quantity.doubleValueForUnit(HKUnit.countUnit()))

     }