Getting the first date of the week with the year and week

I'm trying to get a first date in a week by specifying the year and week in objective-c. I want to take into account the Dutch calendar. Monday is the first day of the week. So, as in MS Outlook, I would like my function to return in 2012-01-02 when I was passing 2012 and 1 for the week number.

Here is what I 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 expect this date to be now 2012-01-02 ... but instead it will be back at 2012-12-30 23:00:00. And when I change comps.weekday to 1, it even returns me 2013-01-05 23:00:00 ?? !!

It makes no sense to me at all. Can someone give me some explanation on this or maybe give me the correct code to use?

Answer:

This solution is for me:

 [components setWeekOfYear:weekNr]; [components setYearForWeekOfYear:year]; [components setWeekday:2]; 
+4
source share

Source: https://habr.com/ru/post/1389347/


All Articles