2011-12-11 156 views

回答

1

看看NSDateComponentsdateByAddingComponents:toDate:options:,這是我使用的,它工作得很好。請務必留意閏年。

-(void)getBetweenDates:(NSDate *)startDate andEndDate:(NSDate *)endDate 
{ 
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
    NSDateComponents *offset = [[NSDateComponents alloc] init]; 
    [offset setDay:1]; 

    NSMutableArray *dates = [NSMutableArray array]; 
    NSDate *curDate = startDate; 
    while([curDate timeIntervalSince1970] <= [endDate timeIntervalSince1970]) 
    { 
     [dates addObject:curDate]; 
     curDate = [calendar dateByAddingComponents:offset toDate:curDate options:0]; 
    } 

    NSLog(@"%@",dates); 

    [offset release]; 
    [calendar release]; 
} 
相關問題