1

如何在ios中获取两个日期之间的天数?

例子:

输入:
 开始日期:2012 年 3 月 11 日
 结束日期:2012 年 5 月 11 日

输出:
 3-11-2012 星期三
 4-11-2012 星期四
 5-11-2012 星期五

谢谢..

4

4 回答 4

11

试试这个,

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    [df setDateFormat:@"yyyy-MM-dd"];
    NSDate *startDate = [df dateFromString:@"2012-07-20"]; // your start date
    NSDate *endDate = [NSDate date]; // your end date
    NSDateComponents *dayDifference = [[NSDateComponents alloc] init];

    NSMutableArray *dates = [[[NSMutableArray alloc] init] autorelease];
    NSUInteger dayOffset = 1;
    NSDate *nextDate = startDate;
    do {
        [dates addObject:nextDate];

        [dayDifference setDay:dayOffset++];
        NSDate *d = [[NSCalendar currentCalendar] dateByAddingComponents:dayDifference toDate:startDate options:0];
        nextDate = d;
    } while([nextDate compare:endDate] == NSOrderedAscending);

    [df setDateStyle:NSDateFormatterFullStyle];
    for (NSDate *date in dates) {
        NSLog(@"%@", [df stringFromDate:date]);
    }
    [df release];

输出是

Friday, July 20, 2012
Saturday, July 21, 2012
Sunday, July 22, 2012
Monday, July 23, 2012
Tuesday, July 24, 2012
于 2012-07-24T05:45:11.880 回答
1

检查此解决方案:

NSDateComponents *components;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd-MM-yyyy"];

NSDate *startDate = [dateFormat dateFromString:@"3-11-2012"];
NSDate *endDate = [dateFormat dateFromString:@"5-11-2012"];

[dateFormat setDateFormat:@"dd-MM-yyyy, EEEE"];

components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit fromDate:startDate toDate:endDate options:0];

int days = [components day];

for (int x = 0; x <= days; x++) {
    NSLog(@"%@", [dateFormat stringFromDate:startDate]);
    startDate = [startDate dateByAddingTimeInterval:(60 * 60 * 24)];
}

输出是:

03-11-2012, Saturday
04-11-2012, Sunday
05-11-2012, Monday
于 2012-07-24T05:45:47.013 回答
1

这是一种方法:

NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setYear:2012];
[dateComponents setMonth:11];
[dateComponents setDay:03];

NSDate *date1 = [gregorianCalendar dateFromComponents:dateComponents];

[dateComponents setYear:2012];
[dateComponents setMonth:11];
[dateComponents setDay:05];

NSDate *date2 = [gregorianCalendar dateFromComponents:dateComponents];

NSDateFormatter *outputDateFormatter = [[NSDateFormatter alloc] init];
[outputDateFormatter setDateFormat:@"dd-MM-yyyy EEEE"];
NSDate *dayDate;

dayDate = date1;
NSLog(@"%@", [outputDateFormatter stringFromDate:dayDate]);

do {
    dayDate = [dayDate dateByAddingTimeInterval:86400];
    NSLog(@"%@", [outputDateFormatter stringFromDate:dayDate]);

} while ([dayDate compare:date2]==NSOrderedAscending);

使用 NSCalendar 为您(和您的用户)提供了更大的显示和操作日期的灵活性。

于 2012-07-24T05:58:40.180 回答
0

这就是你需要的:

NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:@"mm-dd-yyyy"];
NSDate *inputDate = [inputFormatter dateFromString:@"3-11-2012"];

NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"mm-dd-yyyy EEEE"];
NSString *output = [outputFormatter stringFromDate:inputDate];

NSLog(@"%@",output);

[inputFormatter release];
[outputFormatter release];
于 2012-07-24T05:42:59.327 回答