Start with the NSDate
from the collector and continue to add 24 * 60 * 60 seconds to it until Monday appears. Add the total date to the result. Continue to add 7 * 24 * 60 * 60 seconds until the last date you added, and click on the result of the returned list until you get all 64 Mondays. Here's how you find out if NSDate
on Monday:
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *weekdayComponents =[gregorian components:NSWeekdayCalendarUnit fromDate:dateOfInterest]; NSInteger weekday = [weekdayComponents weekday]; if (weekday == 2) ...
EDIT: DaveDeLong pointed out the drawback of the above algorithm: it will shift the time twice during the daylight saving time. Instead of counting seconds manually, use this code to add a day to NSDate
:
NSDate *currentDate = [NSDate date]; NSDateComponents *comps = [[NSDateComponents alloc] init]; [comps setDay:1];
dasblinkenlight
source share