您可以用作:我只给出算法/想法。
创建 stringDate 转换为 NSDate
NSString *range1StartString=@"01/01/05";
NSString *range1EndString=@"01/24/10";
NSString *range2StartString=@"01/25/10";
NSString *range2EndString=@"09/27/10";
NSString *range3StartString=@"09/28/10";
NSString *range3EndString=@"09/30/11";
NSString *range4StartString=@"10/01/11";
NSString *range4EndString=@"01/01/50";
NSString *yourStart=@"01/01/10";
NSString *yourEnd=@"01/31/10";
NSDateFormatter *dateFormatter=[NSDateFormatter new];
[dateFormatter setDateFormat:@"MM/DD/yy"];
NSDate *start1Date=[dateFormatter dateFromString:range1StartString];
NSDate *end1Date=[dateFormatter dateFromString:range1EndString];
NSDate *start2Date=[dateFormatter dateFromString:range2StartString];
NSDate *end2Date=[dateFormatter dateFromString:range2EndString];
NSDate *start3Date=[dateFormatter dateFromString:range3StartString];
NSDate *end3Date=[dateFormatter dateFromString:range3EndString];
NSDate *start4Date=[dateFormatter dateFromString:range4StartString];
NSDate *end4Date=[dateFormatter dateFromString:range4EndString];
NSDate *yourStartDate=[dateFormatter dateFromString:yourStart];
NSDate *yourEndDate=[dateFormatter dateFromString:yourEnd];
在此之后检查范围的漫长过程
//below will find the range for 1.
//1. yourStartDate to start1Date will give negative or positive, discard the negative
//2. yourEndDate to end1Date will give you again -ive or +ve, do same.
//3. Add above +ve values.
使用这种方法检查两个日期是否在范围内并获取它们之间的天数。
-(NSInteger)daysBetweenTwoDates:(NSDate *)fromDateTime andDate:(NSDate*)toDateTime{
NSDate *fromDate;
NSDate *toDate;
NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar rangeOfUnit:NSDayCalendarUnit startDate:&fromDate interval:NULL forDate:fromDateTime];
[calendar rangeOfUnit:NSDayCalendarUnit startDate:&toDate interval:NULL forDate:toDateTime];
NSDateComponents *difference = [calendar components:NSDayCalendarUnit fromDate:fromDate toDate:toDate options:0];
return [difference day]+1;//+1 as if start and end both date are same, so 1 day worked.
}