Skip to content

Instantly share code, notes, and snippets.

@adel-ezzat
Forked from Sunnyztj/gist:9661609
Created February 11, 2021 03:22
Show Gist options
  • Save adel-ezzat/8fa2e9001df7f4e9810bb54b10ac7bec to your computer and use it in GitHub Desktop.
Save adel-ezzat/8fa2e9001df7f4e9810bb54b10ac7bec to your computer and use it in GitHub Desktop.
compare time in objective c
+(int)compareOneDay:(NSDate *)oneDay withAnotherDay:(NSDate *)anotherDay
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSString *oneDayStr = [dateFormatter stringFromDate:oneDay];
NSString *anotherDayStr = [dateFormatter stringFromDate:anotherDay];
NSDate *dateA = [dateFormatter dateFromString:oneDayStr];
NSDate *dateB = [dateFormatter dateFromString:anotherDayStr];
NSComparisonResult result = [dateA compare:dateB];
NSLog(@"date1 : %@, date2 : %@", oneDay, anotherDay);
if (result == NSOrderedDescending) {
//NSLog(@"Date1 is in the future");
return 1;
}
else if (result == NSOrderedAscending){
//NSLog(@"Date1 is in the past");
return -1;
}
//NSLog(@"Both dates are the same");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment