我想在目标c中找到两个日期之间的中间日期。
答案 0 :(得分:1)
看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];
}