如何获取两个日期之间的所有日期?
例如: 开课日期= 2011/01/25 截止日期= 2011/02/03
答案 0 :(得分:2)
1)找出两个日期之间的天数。然后,
for(i=0;i<noofdays;i++)
{
//Find the next date
//add to the array
}
答案 1 :(得分:2)
NSCalendarUnit
用于定义日期和日期之间的步骤。照顾正常化的日期。
iOS 8 API,Swift 2.0
func generateDates(calendarUnit: NSCalendarUnit, startDate: NSDate, endDate: NSDate) -> [NSDate] {
let calendar = NSCalendar.currentCalendar()
let normalizedStartDate = calendar.startOfDayForDate(startDate)
let normalizedEndDate = calendar.startOfDayForDate(endDate)
var dates = [normalizedStartDate]
var currentDate = normalizedStartDate
repeat {
currentDate = calendar.dateByAddingUnit(calendarUnit, value: 1, toDate: currentDate, options: NSCalendarOptions.MatchNextTime)!
dates.append(currentDate)
} while !calendar.isDate(currentDate, inSameDayAsDate: normalizedEndDate)
return dates
}
答案 2 :(得分:0)
查找两个NSDates之间的所有日期:
- (void)cerateDaysArray{
_daysArray = [NSMutableArray new];
NSCalendar *calendar = [[NSCalendaralloc]initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *startDate = [_minDate copy];
NSDateComponents *deltaDays = [NSDateComponents new];
[deltaDays setDay:1];
[_daysArray addObject:startDate];
while ([startDate compare:_maxDate] == NSOrderedAscending) {
startDate = [calendar dateByAddingComponents:deltaDays toDate:startDate options:0];
[_daysArray addObject:startDate];
}
}