Get week number and the date for monday and sunday in the current week (Objective-C on iOS) [closed]

StackOverflow https://stackoverflow.com/questions/8371648

سؤال

how is it possible to get the current weeknumber and the date of monday and sunday in the current week?

هل كانت مفيدة؟

المحلول

I'm not sure how effeicient this is as it's mainly thrown together from, other answers

and a quick skim of the apple docs for

Example:

NSDate *today = [NSDate date];

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
gregorian.firstWeekday = 2; // Sunday = 1, Saturday = 7

NSDateComponents *components = [gregorian components:NSWeekOfYearCalendarUnit fromDate:today];

NSUInteger weekOfYear = [components weekOfYear];

NSDate *mondaysDate = nil;
[gregorian rangeOfUnit:NSWeekCalendarUnit startDate:&mondaysDate interval:NULL forDate:today];

NSLog(@"%ld %@", weekOfYear, mondaysDate);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top