Created
September 12, 2017 17:06
-
-
Save JayachandraA/2f8500cbea4f433b9334051501f77c34 to your computer and use it in GitHub Desktop.
time-falls-between-two-dates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (BOOL)isTimeFallsBetweenTwoDates:(NSDate*)start another:(NSDate*)end{ | |
NSCalendar *gregorian = [[NSCalendar alloc] | |
initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; | |
NSDateComponents *openingTime = [gregorian components:(NSCalendarUnitHour | | |
NSCalendarUnitMinute) fromDate:start]; | |
NSDateComponents *closingTime = [gregorian components:(NSCalendarUnitHour | | |
NSCalendarUnitMinute) fromDate:end]; | |
NSDate *now = [NSDate date]; | |
NSDateComponents *currentTime = [[NSCalendar currentCalendar] components:NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond | |
fromDate:now]; | |
NSMutableArray *times = [@[openingTime, closingTime, currentTime] mutableCopy]; | |
[times sortUsingComparator:^NSComparisonResult(NSDateComponents *t1, NSDateComponents *t2) { | |
if (t1.hour > t2.hour) { | |
return NSOrderedDescending; | |
} | |
if (t1.hour < t2.hour) { | |
return NSOrderedAscending; | |
} | |
// hour is the same | |
if (t1.minute > t2.minute) { | |
return NSOrderedDescending; | |
} | |
if (t1.minute < t2.minute) { | |
return NSOrderedAscending; | |
} | |
// hour and minute are the same | |
if (t1.second > t2.second) { | |
return NSOrderedDescending; | |
} | |
if (t1.second < t2.second) { | |
return NSOrderedAscending; | |
} | |
return NSOrderedSame; | |
}]; | |
if ([times indexOfObject:currentTime] == 1) { | |
NSLog(@"We are Open!"); | |
return TRUE; | |
} else { | |
NSLog(@"Sorry, we are closed!"); | |
return FALSE; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment