// Use compare method.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *startDate = [formatter dateFromString:@"2012-12-07 7:17:58"];
NSDate *endDate = [formatter dateFromString:@"2012-12-07 7:17:59"];
if ([startDate compare: endDate] == NSOrderedDescending) {
NSLog(@"startDate is later than endDate");
} else if ([startDate compare:endDate] == NSOrderedAscending) {
NSLog(@"startDate is earlier than endDate");
} else {
NSLog(@"dates are the same");
}
// Way 2
NSTimeInterval timeDifference = [endDate timeIntervalSinceDate:startDate];
double minutes = timeDifference / 60;
double hours = minutes / 60;
double seconds = timeDifference;
double days = minutes / 1440;
NSLog(@" days = %.0f,hours = %.2f, minutes = %.0f,seconds = %.0f", days, hours, minutes, seconds);
if (seconds >= 1)
NSLog(@"End Date is grater");
else
NSLog(@"Start Date is grater");