-1

Basically I want to know I a user chooses from my settings what day they want a week to start on (This would have a value in the format, @"Mon" for example) and I have an NSMutableArray that holds NSDates (users add theses dates throughout the life of the app) ordered in a chronologically ascending manner, but not necessarily consecutive (there could be day's that users missed and did not add the date to the array), how could I determine that a particular NSDate in the array is part of a different week (relative to the date the user chose for a week to start on)?

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Christian Gossain
  • 5,942
  • 12
  • 53
  • 85
  • 4
    You've asked a slight variation on this question three times now (this question, http://stackoverflow.com/questions/5293543/how-can-i-check-if-an-nsdate-falls-in-between-two-other-nsdates-in-an-nsmutablear, and http://stackoverflow.com/questions/5293211/determining-if-a-day-of-the-week-lies-in-between-two-others-in-an-nsmutablearray, which I answered). Are you sure you're not better off revising one of those questions until you get an acceptable answer? – Tim Mar 14 '11 at 21:43
  • 2
    if you continue deleting and re-asking questions, your account will be suspended. just FYI. – Jeff Atwood Mar 15 '11 at 07:39
  • 3
    you've gotten the (negative) attention of (one of the) founders of this site. It's really not all in good fun. – Kirk Woll Mar 16 '11 at 04:02

2 Answers2

2

This will give you the difference between two dates in weeks:

NSDateComponents *dateDifference = [gregorian components:NSWeekCalendarUnit fromDate:day1 toDate:day2 options:0];
NSUInteger weeksDiff = [dateDifference week];

So if(!weekDiff){ /*same week*/}

Complete example:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *comps1 = [[NSDateComponents alloc] init];
NSDateComponents *comps2 = [[NSDateComponents alloc] init];
[comps1 setDay:5];
[comps2 setDay:12];

NSDate *day1 = [gregorian dateByAddingComponents:comps1 toDate:[NSDate date] options:0];
NSDate *day2 = [gregorian dateByAddingComponents:comps2 toDate:[NSDate date] options:0];


NSDateComponents *dateDifference = [gregorian components:NSWeekCalendarUnit fromDate:day1 toDate:day2 options:0];

NSLog(@"\n%@ \n%@\n%d", day1, day2, [dateDifference week]);
[comps1 release];
[comps2 release];
[gregorian release];

For converting strings to NSDates, see NSDateFormatter

edit as response to comment
if you want to have a week that starts with the first date you pass in, you could instead count the days and check if they have more than 7 days difference.

NSDateComponents *dateDifference = [gregorian components:NSDayCalendarUnit fromDate:day1 toDate:day2 options:0];
NSUInteger daysDiff = [dateDifference day];
if(weekDiff < 7){ /*same week*/}
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
  • And how can I adjust this so that the starting day of a week is not the default. Lets say I wanted to do this relative to Wednesday being the start of the weeks? – Christian Gossain Mar 15 '11 at 00:19
1

Use NSDateComponents. You can get them from date with NSCalendar instance.

hoha
  • 4,418
  • 17
  • 15