The easiest way to get a day/week/month/... timespan for a given date is the
rangeOfUnit
method. For example:
NSDate *date1, *date2;
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *startOfTimespan;
NSTimeInterval lengthOfTimespan;
[calendar rangeOfUnit:NSWeekOfYearCalendarUnit
startDate:&startOfTimespan
interval:&lengthOfTimespan
forDate:date1];
Now startOfTimespan
is the start of the week that contains date1
,
and lengthOfTimespan
is the length of that week. So you can test date2
with
NSTimeInterval diff = [date2 timeIntervalSinceDate:startOfTimespan];
if ( diff >= 0 && diff < lengthOfTimespan) {
// date1 and date2 are in the same week
}