I'd like to compare two different times in an if statement. Fort example
if (10:37 > 09:00) // DO SOMETHING
Is there a way to do this using NSDate and NSDateFormatter?
I'd like to compare two different times in an if statement. Fort example
if (10:37 > 09:00) // DO SOMETHING
Is there a way to do this using NSDate and NSDateFormatter?
You need to use timeIntervalSinceDate
api for comparing two different times.
NSDate *date1;
NSDate *date2;
if([date1 laterDate:date2] == *it will return the later date, either date1 or date2*)
Try this
//Date formatter - to format the sample dates using for comparison
NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init];
[dateformatter setDateFormat:@"HH:MM:SS"];
// Creating 2 sample dates
NSDate *date1 = [dateformatter dateFromString:@"10:10:10"];
NSDate *date2 = [dateformatter dateFromString:@"10:10:20"];
if (NSOrderedDescending == [date1 compare:date2])// Here comparing the dates
{
NSLog(@"date1 later than date 2");
}
else
{
NSLog(@"date2 later than date 1");
}
As I understand your question, you only have times, not dates. Actually than NSDate isn't the best choice, as it represents exactly one moment in time for all eternity.
If you only have times or recurring dates, NSDateComponents are the better choice. You can define what components is used, in your case hour and minute.
NSDateComponents have one caveat: no compare:
method, but you can easily compare as
if (date1Comps.hour < date2Comps.hour) {
NSLog(@"date 1 earlier");
} else if(date1Comps.hour > date2Comps.hour) {
NSLog(@"date 2 earlier");
} else {
if (date1Comps.minute < date2Comps.minute) {
NSLog(@"date 1 earlier");
} else if(date1Comps.minute > date2Comps.minute){
NSLog(@"date 2 earlier");
} else {
NSLog(@"same hour, same minute");
}
}
here is the full code including creating components from dates.
NSString *date1String = @"2014-07-18T10:30";
NSString *date2String = @"2014-07-18T10:45";
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy-MM-dd'T'HH:mm"];
NSDate *date1 = [f dateFromString:date1String];
NSDate *date2 = [f dateFromString:date2String];
NSDateComponents *date1Comps = [[NSCalendar currentCalendar] components:(NSCalendarUnitHour|NSCalendarUnitMinute) fromDate:date1];
NSDateComponents *date2Comps = [[NSCalendar currentCalendar] components:(NSCalendarUnitHour|NSCalendarUnitMinute) fromDate:date2];
if (date1Comps.hour < date2Comps.hour) {
NSLog(@"date 1 earlier");
} else if(date1Comps.hour > date2Comps.hour) {
NSLog(@"date 2 earlier");
} else {
if (date1Comps.minute < date2Comps.minute) {
NSLog(@"date 1 earlier");
} else if(date1Comps.minute > date2Comps.minute){
NSLog(@"date 2 earlier");
} else {
NSLog(@"same hour, same minute");
}
}
or similar: order the date components in an array
NSArray *times = [@[date1Comps, date2Comps] sortedArrayUsingComparator:^NSComparisonResult(NSDateComponents *date1Comps, NSDateComponents *date2Comps) {
if (date1Comps.hour < date2Comps.hour) {
return NSOrderedAscending;
} else if(date1Comps.hour > date2Comps.hour) {
return NSOrderedDescending;
} else {
if (date1Comps.minute < date2Comps.minute) {
return NSOrderedAscending;
} else if(date1Comps.minute > date2Comps.minute){
return NSOrderedDescending;
}
}
return NSOrderedSame;
}];
You can save this comparison block and reuse it. Than it won't matter much that there is no easy compare method.
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]];
NSInteger todayHour = [components hour];
NSInteger currentMinute = [components minute];
if (currentHour < 7 || (currentHour > 21 || currentHour == 21 && (currentMinute > 0 || currentMinute > 0))) {
// Do your task
}
This may helps you :)