So how do you do this:
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger desiredComponents = (NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit);
NSDate *firstDate = ...;
NSDate *secondDate = ...;
NSDateComponents *firstComponents = [calendar components:desiredComponents fromDate:firstDate];
NSDateComponents *secondComponents = [calendar components:desiredComponents fromDate:secondDate];
NSDate *truncatedFirst = [calendar dateFromComponents:firstComponents];
NSDate *truncatedSecond = [calendar dateFromComponents:secondComponents];
NSComparisonResult result = [truncatedFirst compare:truncatedSecond];
if (result == NSOrderedAscending) {
} else if (result == NSOrderedDescending) {
} else {
}
Basically, we take two dates, beat their hours-minutes-seconds and turn them back to dates. Then we compare these dates (which no longer have a time component, just a date component) and see how they compare with eachother.
WARNING: typed in a browser and not compiled. Warning
source
share