2011-12-11 63 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]; 
} 
関連する問題