你几乎明白了;只是几个修改:
int numberOfDays=30;
NSDate *startDate=[NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *offset = [[NSDateComponents alloc] init];
NSMutableArray* dates = [NSMutableArray arrayWithObject:startDate];
for (int i = 1; i < numberOfDays; i++) {
[offset setDay:i];
NSDate *nextDay = [calendar dateByAddingComponents:offset toDate:startDate options:0];
[dates addObject:nextDay];
}
[offset release];
NSLog(@"%@",dates);
这将创建一个对象数组NSDate
。在我的机器上,这会记录:
EmptyFoundation[4302:903] (
"2011-02-16 16:16:26 -0800",
"2011-02-17 16:16:26 -0800",
"2011-02-18 16:16:26 -0800",
"2011-02-19 16:16:26 -0800",
"2011-02-20 16:16:26 -0800",
"2011-02-21 16:16:26 -0800",
"2011-02-22 16:16:26 -0800",
"2011-02-23 16:16:26 -0800",
"2011-02-24 16:16:26 -0800",
"2011-02-25 16:16:26 -0800",
"2011-02-26 16:16:26 -0800",
"2011-02-27 16:16:26 -0800",
"2011-02-28 16:16:26 -0800",
"2011-03-01 16:16:26 -0800",
"2011-03-02 16:16:26 -0800",
"2011-03-03 16:16:26 -0800",
"2011-03-04 16:16:26 -0800",
"2011-03-05 16:16:26 -0800",
"2011-03-06 16:16:26 -0800",
"2011-03-07 16:16:26 -0800",
"2011-03-08 16:16:26 -0800",
"2011-03-09 16:16:26 -0800",
"2011-03-10 16:16:26 -0800",
"2011-03-11 16:16:26 -0800",
"2011-03-12 16:16:26 -0800",
"2011-03-13 16:16:26 -0700",
"2011-03-14 16:16:26 -0700",
"2011-03-15 16:16:26 -0700",
"2011-03-16 16:16:26 -0700",
"2011-03-17 16:16:26 -0700"
)
请注意时区偏移如何在 3 月 13 日从 更改-0800
为-0700
。那是夏令时。:)