打印2个给定日期之间的日期

时间:2014-10-17 07:07:18

标签: ios nsdate

我想打印两个给定日期之间的中间日期。

例如fromDate=12/10/2014toDate=15/10/2014然后我想要这个,

12/10/2014

13/10/2014

14/10/2014

15/10/2014.

任何人都可以指导我吗?

1 个答案:

答案 0 :(得分:0)

试试这个

//datemin= fromdate , datemax=todate
    NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
    NSDateComponents *days = [[NSDateComponents alloc] init];
    NSInteger dayCount = 0;
    while ( TRUE ) {
        [days setDay: ++dayCount];
        NSDate *date = [gregorianCalendar dateByAddingComponents: days toDate: datemin options: 0];
        if ( [date compare: dateMax] == NSOrderedDescending )
            break;
        NSLog(@"%@",date);
        // Do something with date like add it to an array, etc.
    }