Skip to content

Instantly share code, notes, and snippets.

@mmackh
Last active December 19, 2015 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmackh/5978268 to your computer and use it in GitHub Desktop.
Save mmackh/5978268 to your computer and use it in GitHub Desktop.
Find the number of overlapping days of 4 NSDates
- (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