Created
May 29, 2009 06:55
-
-
Save brwnx/119821 to your computer and use it in GitHub Desktop.
Get Array of all NSDates in a given year
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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