Would be happy to hear of suggestions re how to improve / shorten this method. In short needing to:
- Find the next date for which it's day of week (e.g. Wed) matches what is passed into the method.
- For example the next WED from a given date (and including that given date)
Code Below:
- (NSDate*)DateFromNextWeekDay:(NSInteger)weekDay FromDate:(NSDate*)fromDate {
// Returns the next week day, as specified by "weekDay", from the specified "fromDate"
NSDate *fromDateMidday = [[NSDate date] dateBySettingHour:12 andMinute:0];
NSDate *dateCounter = [[fromDateMidday copy] dateByAddingTimeInterval:-86400]; // Take 1 day away, which will get incremented in the loop
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSInteger day;
do{
dateCounter = [dateCounter dateByAddingTimeInterval:86400];
unsigned units = NSWeekdayCalendarUnit;
NSDateComponents *components = [gregorian components:units fromDate:dateCounter];
day = [components weekday];
} while(day != weekDay);
[gregorian release];
return dateCounter;
}
thanks