Reputation: 1292
I'm trying to generate an array of 30 days from the current date and put them into an array formatted correctly. I currently have a working generater to get 30 days from the current date and put it into an array but I don't know how to use NSDate formatter for an array. I know how to use the formatter for a single item but how can I format all the dates in my array? The format that I would like is (Month Day). Here's my code:
NSMutableArray* dates = [[NSMutableArray alloc] init];
int numberOfDays=30;
NSDate *startDate=[NSDate date];
NSDate *tempDate=[startDate copy];
for (int i=0;i<numberOfDays;i++) {
NSLog(@"%@",tempDate.description);
tempDate=[tempDate dateByAddingTimeInterval:(60*60*24)];
[dates addObject:tempDate.description];
}
NSLog(@"%@",dates);
Any help would be appreciated, Thanks.
Upvotes: 0
Views: 2274
Reputation: 12787
use like this,
NSMutableArray* dates = [[NSMutableArray alloc] init];
int numberOfDays=30;
NSDate *startDate=[NSDate date];
NSDate *tempDate=[startDate copy];
for (int i=0;i<numberOfDays;i++) {
NSLog(@"%@",tempDate.description);
tempDate=[tempDate dateByAddingTimeInterval:(60*60*24)];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd"];
NSString *dateString = [dateFormat stringFromDate:tempDate];
tempDate=[dateFormat dateFromString:dateString];
[dateFormat release];
[dates addObject:tempDate.description];
}
NSLog(@"%@",dates);
Upvotes: 2
Reputation: 3960
i suggest you to give the NSDate formatter
to your date first and then give those objects to the NSMutableArray
and whenever you fetch the object from an array you get the required format.
Hope you get my point...Good Luck!
Upvotes: 3