Swift:检查两个NSDates是否相同
真的很简单,我相信这是一个很快的bug。我试图检查2个NSDates是否相同。这种说法从来没有通行证甚至以为打印输出低于Swift:检查两个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())))
}
手绘POP练习13时56分50秒+0000手绘POP练习13时56分50秒+0000
手绘POP练习13点56分50秒+0000 2015年3月31日13点56分50秒+0000
2015年3月31日13点56分50秒+0000手绘POP练习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()))
}
检查日期是否相等的最简单方法是使用NSDate.isEqualToDate()方法。
或
NSCalendar.currentCalendar().compareDate(date1, toDate: date2, toUnitGranularity: NSCalendarUnit.CalendarUnitSecond)
如果使用相当多的日期计算校验this库。
你可以使用这个扩展:
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为你的日期
可悲的是不工作,它认为日期是dif – Burf2000 2015-04-01 14:07:33
我居然发现这个新的方法来解决这个问题
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()))
}
所以实际上你想检查两个NSDates是否在*相同的小时*内,如果它们是相同的。如果你在你的问题中澄清这个问题,对未来的读者会很有用。 – 2015-04-01 14:24:05
不,但这是一个解决方法,看起来子秒可能是不同的事件,虽然时间是通过healthkit – Burf2000 2015-04-01 14:26:23
NSDate代表绝对时间点。如果2个NSDate相差0.01秒,那么它们是不同的。 – 2015-04-01 14:27:36
你应该能够比较==操作符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")
}
如果我改变结束日期
dateComponents.second = 5
endDate = calendar!.dateByAddingComponents(dateComponents, toDate: startDate, options: nil)
if(startDate == endDate) // false here
{
println("equal")
}
刚才试了很简单的一个,它似乎有可能只是NSDate的()== NSDate的() – 2015-04-01 14:12:01