4

I'm trying to get the first date in a week giving a year and weeknumber in objective-c. I want to take the dutch calendar into account. Monday is the first day in the week here. So just as it is in MS Outlook I would like my function to return 2012-01-02 when i pass 2012 and 1 for the week number to it.

This is what I have tried:

NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *comps = [[NSDateComponents alloc] init];

comps.year = 2012;
comps.weekOfYear = 1;
comps.weekday = 2;

NSDate *date = [cal dateFromComponents:comps];

I would expect that date would now be 2012-01-02 ..., but instead it returns 2012-12-30 23:00:00. And when I change comps.weekday to 1, it is even returning me 2013-01-05 23:00:00??!!

This makes no sense to me at all. Could anyone provide me some explanation on this or perhaps give me the correct code to use?

ANSWER:

This was the solution for me:

[components setWeekOfYear:weekNr];
[components setYearForWeekOfYear:year];
[components setWeekday:2];

0 Answers0