Viraj
Viraj

Reputation: 209

First date(NSDate) of the given week

I have a week number in int and I want to get the first date( NSDate value ) of that week.

For an Example: First date ( sunday ) of the 21st week of 2011 is 2011-05-22

I'm trying with NSCalendar, NSDateComponents to achieve this. If anybody knows how to achieve this really appreciate your help.

Upvotes: 0

Views: 1410

Answers (1)

Vladimir
Vladimir

Reputation: 170839

Try something like:

NSDate *now = [NSDate date];
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *comp = [gregorian components:NSYearCalendarUnit fromDate:now];
[comp setWeekday:1];
[comp setWeek: 21];
NSDate *resultDate = [gregorian dateFromComponents:comp];

Upvotes: 4

Related Questions