1

Hello I have a date string like this.11/14/2016. What I want to do is check it whether is it

Today or This week or This month. I can compare today and the month. But how can I check whether this date belongs to "This Week"

Please help me. Thanks

user1960169
  • 3,533
  • 12
  • 39
  • 61

2 Answers2

3

Given two dates,

NSDate *now = [NSDate date];
NSDate *then = // some other date

Find out if they're in the same week with:

BOOL sameWeek = [[NSCalendar currentCalendar] isDate:then equalToDate:now 
    toUnitGranularity:NSCalendarUnitWeekOfYear];

That line asks if they're "equal" with a granularity of a week, meaning that they're "equal" if they're in the same week.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
0

Try below code,

- (void)thisWeek:(NSDate *)date
{
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *todaysComponents = [gregorian components:NSCalendarUnitWeekOfYear fromDate:[NSDate date]];
    NSUInteger todaysWeek = [todaysComponents weekOfYear];
    NSDateComponents *otherComponents = [gregorian components:NSCalendarUnitWeekOfYear fromDate:date];
    NSUInteger datesWeek = [otherComponents weekOfYear];

    //NSLog(@"Date %@",date);
    if(todaysWeek==datesWeek){
        NSLog(@"Date is in this week");
    }else if(todaysWeek+1==datesWeek){
        NSLog(@"Date is in next week");
    }

}
Ashish Sahu
  • 388
  • 3
  • 16