0

我有几个日期范围:

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 天)。

这让我困惑了一段时间。任何帮助将不胜感激。

4

3 回答 3

0

您可以用作:我只给出算法/想法。

创建 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.
}
于 2013-01-28T07:31:29.153 回答
0

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

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

亚伦

于 2013-01-28T07:12:07.130 回答
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);

WherefirstDatesecondDateNSDate对象。希望能帮助到你

于 2013-01-28T07:21:42.790 回答