Reputation: 293
I want to calculate the week numbers in a given month. example the month july 2012 should have week number 26, 27, 28, 29, 30 and 31. I cooked up this snippet, but I looking for a workable way to get the preceding and succeeding week numbers. Any idea's.
Thanks, Darrell.
//Get current week number
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:NSWeekCalendarUnit fromDate:[NSDate date]];
NSInteger week = [components week];
NSLog(@"Week nummer: %d", (int)week);
//Get week numbers in month
[components weekOfYear];
NSRange weeksInMonth = [cal rangeOfUnit:NSWeekCalendarUnit
inUnit:NSMonthCalendarUnit
forDate:[cal dateFromComponents:components]];
NSLog(@"Weken in maand: %lu", weeksInMonth.length);
Upvotes: 1
Views: 2222
Reputation: 27073
Function:
- (NSArray *)weeksOfMonth:(int)month inYear:(int)year
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setMonth:month];
[components setYear:year];
NSRange range = [calendar rangeOfUnit:NSDayCalendarUnit
inUnit:NSMonthCalendarUnit
forDate:[calendar dateFromComponents:components]];
calendar = [NSCalendar currentCalendar];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSMutableSet *weeks = [[NSMutableSet alloc] init ];
for(int i = 0; i < range.length; i++)
{
NSString *temp = [NSString stringWithFormat:@"%4d-%2d-%2d",year,month,range.location+i];
NSDate *date = [dateFormatter dateFromString:temp ];
int week = [[calendar components: NSWeekOfYearCalendarUnit fromDate:date] weekOfYear];
[weeks addObject:[NSNumber numberWithInt:week]];
}
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"" ascending:YES];
NSArray *descriptors = [[NSArray alloc] initWithObjects:descriptor, nil];
return [weeks sortedArrayUsingDescriptors:descriptors];
}
Usage:
NSArray *weeks = [self weeksOfMonth:7 inYear:2012];
NSLog(@"%@",weeks);
Output:
{(
27,
28,
29,
30,
31
)}
Upvotes: 4
Reputation: 293
The solution Anne sugested works for me, but the output is not sorted like her output. I needed to convert to an array and sort like:
// Sorting everything.
NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES];
NSArray *weeks = [[self weeksOfMonth:7 inYear:2012]allObjects];
NSArray *sorters = [[NSArray alloc] initWithObjects:sorter, nil];
NSArray *sortedArray = [weeks sortedArrayUsingDescriptors:sorters];
for (NSNumber *element in sortedArray)
{
NSLog(@"%i",element.intValue);
}
Is this the logical step of is there a more sexy way to do this?
Upvotes: 1
Reputation: 1027
int year = 2012;
int month = 7;
NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *date = nil;
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setYear:year];
[components setMonth:month];
date = [cal dateFromComponents:components];
NSLog(@"starting week # %d = %@", [[cal components:NSWeekCalendarUnit fromDate:date] week], date);
[components setMonth:month + 1];
[components setDay:-1];
date = [cal dateFromComponents:components];
NSLog(@"ending week # %d = %@", [[cal components:NSWeekCalendarUnit fromDate:date] week], date);
Upvotes: 2