I hope this is just a learning curve I’m going through.
I create an arbitrary date. I want to find the Saturday following the third Friday.
NSLog(@"Date: %@", [date description]);
NSCalendar *calendar = [NSCalendar currentCalendar];
NSTimeZone *zone = [NSTimeZone timeZoneWithName:@"EST"];
[calendar setTimeZone:zone];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:date];
[components setWeekday:6]; // 1 = Sunday ... 7 = Saturday
[components setWeekdayOrdinal:3]; // 3rd Friday
resultDate = [calendar dateFromComponents:components];
NSLog(@"Date: %@", [resultDate description]);
And the output:
Date: 2010-11-01 05:00:00 GMT
Date: 2010-11-01 05:00:00 GMT
Why? How can I fix this?
Change the components to:This works:
Hope this helps.