0
私は2つの日付間の中間日付を目的地cで検索したいと思います。Xcode - 2つの日付間の中間日付を見つける方法
私は2つの日付間の中間日付を目的地cで検索したいと思います。Xcode - 2つの日付間の中間日付を見つける方法
NSDateComponents
とdateByAddingComponents: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];
}
Objective-CまたはC/C++の回答をお探しですか? XCodeは両方のコンパイラを提供しますが、答えは異なります。 – dasblinkenlight
私はObjective-Cの答えを探しています。 –