Obtenez le numéro de la semaine et la date de lundi et dimanche dans la semaine en cours (Objective-C sur iOS) [fermé]

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

Question

comment est-il possible d'obtenir le numéro de la semaine en cours et la date du lundi et dimanche dans la semaine en cours?

Était-ce utile?

La solution

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);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top