Josh Kahane
Josh Kahane

Reputation: 17159

NSDateComponents setWeek - Find this week

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?

Upvotes: 2

Views: 1631

Answers (1)

vodkhang
vodkhang

Reputation: 18741

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

Upvotes: 4

Related Questions