views:

46

answers:

1

Is it possible to find the current week of the year using NSDateComponents and setWeek: ? As setWeek uses a number form 1 - 52 every week in the year, how can I find the number representing this week?

A: 

If I understand your question correctly, I think the solution is simple, you create a NSDateComponents of the current date and you get the week.

    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSWeekCalendarUnit;
    NSDateComponents *dateComponents = [calendar components:unitFlags
                                                   fromDate:[NSDate date] // for current date
                                                    options:0];

    NSUInteger week = [dateComponents week];  // here is your week
vodkhang
Ahh! I see, well I just found out, before looking back here, I could do it this way:
Josh Kahane
NSDateFormatter *weekFormatter = [[NSDateFormatter alloc] init]; [weekFormatter setDateFormat:@"w"]; NSString *weekDateString = [weekFormatter stringFromDate:curDate]; [weekFormatter release];
Josh Kahane