Skip to content

Instantly share code, notes, and snippets.

@brwnx
Created May 29, 2009 06:55
Show Gist options
  • Save brwnx/119821 to your computer and use it in GitHub Desktop.
Save brwnx/119821 to your computer and use it in GitHub Desktop.
Get Array of all NSDates in a given year
- (NSArray *) dates:(int) inYear
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSMutableArray* result = [[NSMutableArray alloc]init];
NSDate* startDate;
NSDateComponents* comps = [[NSDateComponents alloc] init];
[comps setDay:1];
[comps setMonth:1];
[comps setYear:inYear];
startDate = [calendar dateFromComponents:comps];
[comps release];
int daysToAdd = 1;
int yearInLoop = [[startDate descriptionWithCalendarFormat:@"%Y" timeZone:nil locale:nil]intValue];
[result addObject:startDate];
while(YES){
startDate = [startDate addTimeInterval:60*60*24*daysToAdd];
yearInLoop = [[startDate descriptionWithCalendarFormat:@"%Y" timeZone:nil locale:nil]intValue];
if(yearInLoop==inYear){
[result addObject:startDate];
}else{
break;
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment