how is it possible to get the current weeknumber and the date of monday and sunday in the current week?
Asked
Active
Viewed 6,249 times
1
-
2To do this you read the specs for the components you've listed above. And possibly do a little actual programming. – Hot Licks Dec 04 '11 at 00:04
1 Answers
13
I'm not sure how effeicient this is as it's mainly thrown together from, other answers
- NSCalendar first day of week
- How to check what day of the week it is (i.e. Tues, Fri?) and compare two NSDates?)
and a quick skim of the apple docs for
Example:
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
gregorian.firstWeekday = 2; // Sunday = 1, Saturday = 7
NSDateComponents *components = [gregorian components:NSWeekOfYearCalendarUnit fromDate:today];
NSUInteger weekOfYear = [components weekOfYear];
NSDate *mondaysDate = nil;
[gregorian rangeOfUnit:NSWeekCalendarUnit startDate:&mondaysDate interval:NULL forDate:today];
NSLog(@"%ld %@", weekOfYear, mondaysDate);