Objective-C:日期范围之间相交的计数天数

时间:2013-01-28 06:37:31

标签: objective-c xcode

我有几个日期范围:

range1: 01/01/05 to 01/24/10,
range2: 01/25/10 to 09/27/10,
range3: 09/28/10 to 09/30/11,
range4: 10/01/11 to 01/01/50;

用户输入以下开始日期和结束日期:01/01/10至01/31/10。如何计算此用户输入日期范围与上述各个日期范围相交的天数? (例如,在这种情况下,我的输出应该是range1中的24天和range2中的7天)。

这一直困扰着我一段时间。任何帮助都会非常感激。

3 个答案:

答案 0 :(得分:0)

一个相当容易实现的解决方案是创建NSNumber数字“日期”对象(使用Julian days - http://en.wikipedia.org/wiki/Julian_day) - 仅限整数。然后你可以使用NSSet的方法来执行交集计算。

我在这里发布了一些代码(How get a datetime column in SQLite with Objective C),用于将NSDates转换为Julian天或从Julian天转换。

亚伦

答案 1 :(得分:0)

我正在使用此代码:

NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setTimeZone:[NSTimeZone systemTimeZone]];
NSDateComponents *cmp1 = [calendar components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:firstDate];
NSDateComponents *cmp2 = [calendar components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:secondDate];

NSLog(@"RANGE BETWEEN FIRST DATE AND SECOND DATE %d": cmp1.day-cmp2.day);

其中firstDatesecondDateNSDate个对象。希望它有所帮助

答案 2 :(得分:0)

你可以使用as:我只给出算法/想法。

创建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.
}