I'm trying to compare two dates by Day in swift. Following this solution wrote the following code:
let workoutdate: NSDate = participantdata[self.lastindexchecked].workout!.start!
let todaysdate = NSDate()
let order = NSCalendar.currentCalendar().compareDate(todaysdate, toDate: workoutdate, toUnitGranularity: .Day)
switch order {
case .OrderedDescending:
print("DESCENDING")
case .OrderedAscending:
print("ASCENDING")
case .OrderedSame:
print("SAME")
}
However, this code does not seem to always be returning the desired result.
todaysdate is equal to:
2016-08-20 20:51:26 +0000
When workoutdate is equal to :
2016-08-20 00:14:53 +0000
The result is "DESCENDING"
However, when the workout date is set to a different one, eg,
2016-08-20 04:00:00 +0000
The result is indeed "SAME" as predicted.
Anyone have any idea what is going on?