Last active
December 19, 2015 15:39
-
-
Save mmackh/5978268 to your computer and use it in GitHub Desktop.
Find the number of overlapping days of 4 NSDates
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) isBetweenBeginDate:(NSDate *)beginDate endDate:(NSDate *)endDate | |
{ | |
return ([self compare:beginDate] == NSOrderedDescending || [self compare:beginDate] == NSOrderedSame) && ([self compare:endDate] == NSOrderedAscending); | |
} | |
- (NSInteger)distanceInDaysToDate:(NSDate *)anotherDate | |
{ | |
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; | |
NSDateComponents *components = [gregorianCalendar components:NSDayCalendarUnit fromDate:self toDate:anotherDate options:0]; | |
return components.day; | |
} | |
+ (NSInteger) countOverlappingDaysOfFirstStartDate:(NSDate *)firstStartDate firstEndDate:(NSDate *)firstEndDate secondStartDate:(NSDate *)secondStartDate secondEndDate:(NSDate *)secondEndDate | |
{ | |
NSArray *dates = @[firstStartDate,firstEndDate,secondStartDate,secondEndDate]; | |
NSArray *sortedDates = [dates sortedArrayUsingComparator:^NSComparisonResult(NSDate *a, NSDate *b) | |
{ | |
return [a compare:b]; | |
}]; | |
if (![firstStartDate isBetweenBeginDate:secondStartDate endDate:secondEndDate] && ![firstEndDate isBetweenBeginDate:secondStartDate endDate:secondEndDate]) return 0; | |
return [sortedDates[1] distanceInDaysToDate:sortedDates[2]]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment