Deepak Thakur
Deepak Thakur

Reputation: 3691

Check if NSDate is in next week

I tried the following code, but I do not get a condition where the date is in next week. How will I know that the date which is parameter in the function falls in next week? Following code always returns 1.

- (NSInteger)thisW:(NSDate *)date
{
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];

    NSDateComponents *todaysComponents =
    [gregorian components:NSYearCalendarUnit fromDate:[NSDate date]];

    NSUInteger todaysWeek = [todaysComponents weekOfYear];


    NSDateComponents *otherComponents =
    [gregorian components:NSYearCalendarUnit fromDate:date];

    NSUInteger datesWeek = [otherComponents weekOfYear];
    //NSLog(@"Date %@",date);
    if(todaysWeek==datesWeek){
        //NSLog(@"Date is in this week");
        return 1;
    }else if(todaysWeek+1==datesWeek){
        //NSLog(@"Date is in next week");
        return 2;
    } else {
        return 0;
    }

}

Upvotes: 1

Views: 99

Answers (2)

Riddick
Riddick

Reputation: 400

Using datecomponents for week calculations will give you problems when the dates are close to year's end, i.e. "The first week of the year is designated to be the week containing the first Thursday of the year.(ISO 8601)"

I find it easier to compare dates with certain granularity, weekly in this case (the following code detects if a date is more than one week in the past, last week, this week, next week or further in the future)

-(EventWeekRange)numberOfWeeksFromTodayToEvent:(NSDate *)eventDate {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSComparisonResult comparison = [calendar compareDate:[NSDate date] toDate:eventDate toUnitGranularity:NSCalendarUnitWeekOfYear];
    if (comparison == NSOrderedSame) {
        return RangeThisWeek;
    } else if (comparison == NSOrderedAscending) { // The event date is in the future
        // Advance today's date one week to check if this new date is in the same week as the event
        NSDate *todaysNextWeek = [[NSDate date]dateByAddingTimeInterval:60*60*24*7];
        if ([calendar compareDate:todaysNextWeek toDate:eventDate toUnitGranularity:NSCalendarUnitWeekOfYear] == NSOrderedSame) {
            return RangeNextWeek;
        } else {
            return RangeLater;
        }
    } else { // The event date is in the past
        // Advance the event's date one week to check if this new date is in the same week as today
        NSDate *eventsNextWeek = [eventDate dateByAddingTimeInterval:60*60*24*7];
        if ([calendar compareDate:eventsNextWeek toDate:[NSDate date] toUnitGranularity:NSCalendarUnitWeekOfYear] == NSOrderedSame) {
            return RangeLastWeek;
        } else {
            return RangeEarlier;
        }
    }
}

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726599

You need to pass NSCalendarUnitWeekOfYear when extracting date components:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *todaysComponents = [gregorian components:NSCalendarUnitWeekOfYear fromDate:[NSDate date]];
NSUInteger todaysWeek = [todaysComponents weekOfYear];
NSDateComponents *otherComponents = [gregorian NSCalendarUnitWeekOfYear fromDate:date];
NSUInteger datesWeek = [otherComponents weekOfYear];

Upvotes: 1

Related Questions