NSDateComponents setWeek - Find this week
-
30-09-2019 - |
Question
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?
Solution
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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow