Current Week Start and End Date

Rushabh picture Rushabh · Jul 27, 2012 · Viewed 31.8k times · Source

I want to get the current week start and end date and I also want to use the previous week start and end date and next week of the start and end date in current month.

Thanks in Advance.

Answer

vikingosegundo picture vikingosegundo · Jun 7, 2013

rangeOfUnit:startDate:interval:forDate:. It gives you the start and the interval for a certain time unit. With it it is easy to find the start of the week in the used calendar and add the range-1 to get the latest second in that week.

NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *now = [NSDate date];
NSDate *startOfTheWeek;
NSDate *endOfWeek;
NSTimeInterval interval;
[cal rangeOfUnit:NSWeekCalendarUnit 
       startDate:&startOfTheWeek 
        interval:&interval 
         forDate:now];
//startOfWeek holds now the first day of the week, according to locale (monday vs. sunday)

endOfWeek = [startOfTheWeek dateByAddingTimeInterval:interval-1];
// holds 23:59:59 of last day in week.