次のように使用できます: 私はアルゴリズム/アイデアのみを提供しています。
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.
この方法を使用して、2 つの日付が範囲内にあるかどうかを確認し、その間の日数を取得します。
-(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.
}