I need to know if two NSDate instances are both from the same day.
Is there an easier/better way to do it than getting the NSDateComponents and comparing day/month/y
The following will test whether two dates represent the same day in a given era (which is sufficient in many cases):
- (BOOL)isDate:(NSDate *)date1 sameDayAsDate:(NSDate *)date2 {
NSCalendar *calendar = [NSCalendar currentCalendar];
int diff = [calendar ordinalityOfUnit:NSDayCalendarUnit
inUnit:NSEraCalendarUnit
forDate:date1] -
[calendar ordinalityOfUnit:NSDayCalendarUnit
inUnit:NSEraCalendarUnit
forDate:date2];
return 0 == diff;
}