We can use "compare"
function of "NSComparisonResult" Class to get order of the NSDate.
switch ([dateOne compare:dateTwo]){case NSOrderedAscending: NSLog(@"NSOrderedAscending"); break; case NSOrderedSame: NSLog(@"NSOrderedSame"); break;case NSOrderedDescending: NSLog(@"NSOrderedDescending"); break;
}
or if we want to compare only date year month and day, we can use.
NSCalendar and NSDateComponents.// Get the current calendarNSCalendar* calendar = [NSCalendar currentCalendar];// Set the flags you want to match unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;// Set the componentsNSDateComponents* comp1 = [calendar components:unitFlags fromDate:dateOne];NSDateComponents* comp2 = [calendar components:unitFlags fromDate:dateTwo];// Compare the date components.if ([comp1 day]==[comp2 day] && [comp1 month]==[comp2 month] && [comp1 year]==[comp2 year])NSLog(@"Dates match.");
No comments:
Post a Comment