0

I have several date ranges:

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;

The user inputs the following start date and end date: 01/01/10 to 01/31/10. How can I calculate how many days of this user-inputted date range intersect with the respective date ranges above? (e.g., in this case, my output should that there are 24 days in range1 and 7 days in range2).

This has been puzzling me for a while now. Any help would be GREATLY appreciated.

Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
George Friday
  • 585
  • 1
  • 6
  • 22
  • 2
    In order for us to help you, please post the code that you have tried and tell us what is wrong with it. – lnafziger Jan 28 '13 at 06:41
  • possible duplicate of [How can I calculate the difference between two dates?](http://stackoverflow.com/questions/4371757/how-can-i-calculate-the-difference-between-two-dates) – Carl Norum Jan 28 '13 at 06:41
  • 2
    @CarlNorum: How can this be a dupe of that? he doesn't want to find the diff. He want to find common between those given range. – Anoop Vaidya Jan 28 '13 at 06:43
  • 1
    How are you going to do that? By taking some differences. You see that the ranges listed don't overlap, right? – Carl Norum Jan 28 '13 at 06:47
  • Some thing is similar but it is differnt, otherwise every questions on NSDate and NSDateComponents can be said as similar :p – Anoop Vaidya Jan 28 '13 at 06:48
  • @CarlNorum: Counting the days between two different dates is only part of the problem. He will also need to create `NSDate`'s and will need to identify where the user input falls within those date ranges. It might be easy for those of us well versed in iOS's date functions, but it is pretty cryptic to a beginner. – lnafziger Jan 28 '13 at 07:08
  • Even i am trying to solve for past 15min, but too much of codes and logic, getting messed up :( – Anoop Vaidya Jan 28 '13 at 07:10
  • Oh, and since this is only tagged `objective-c`, we don't even know if he is targeting iOS/MacOS. He might not even have `NSDate` available. ... :-) – lnafziger Jan 28 '13 at 07:14

3 Answers3

0

One fairly easy to implement solution would be to create NSNumber numeric "date" objects (using Julian days - http://en.wikipedia.org/wiki/Julian_day) - integers only. Then you could use NSSet's methods to perform intersection calculations.

I posted some code here (How get a datetime column in SQLite with Objective C) for converting NSDates to/from Julian days.

Aaron

Community
  • 1
  • 1
xyzzycoder
  • 1,831
  • 13
  • 19
0

i'm using this code:

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);

Where firstDate and secondDate are NSDate objects. Hope it helps

Timur Mustafaev
  • 4,869
  • 9
  • 63
  • 109
  • Ummm, this won't work since you are using only the "day" component. So, for instance, if you have "2/28/10" and "3/1/10" it will log "-27" (28-1)... – lnafziger Jan 28 '13 at 07:31
0

You can use as: I am giving only algorithm/idea.

Create stringDate convert to 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];

After this a long process to check for range

 //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.

use this method of check if two dates are in range and get the days between them.

-(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.
}
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140