如何在iOS中的不同日期计算两次之间的差异?

时间:2015-06-30 10:12:27

标签: ios iphone

starttime = 23:00 pm 结束时间=早上5点

我需要开始时间到结束时间的总和时间 我正在尝试这段代码但对我没有帮助

NSDateFormatter *formatter = [NSDateFormatter new];
        [formatter setDateFormat:@"HH"];

        NSDate *currentTime = [NSDate date];
        NSDate *openTime = [formatter dateFromString:start];
        NSDate *closeTime = [formatter dateFromString:end ];
        NSCalendar *calender = [NSCalendar currentCalendar];
        NSDateComponents *currentTimeComponents = [calender components:NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:currentTime];
        NSDateComponents *openTimeComponents = [calender components:NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:openTime];
        NSDateComponents *closeTimeComponents = [calender components:NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:closeTime];

        NSUInteger currentTimeSeconds = currentTimeComponents.hour;

        NSUInteger openTimeSeconds = openTimeComponents.hour;

        NSUInteger closeTimeSeconds = closeTimeComponents.hour;

        if (currentTimeSeconds>openTimeSeconds && currentTimeSeconds<closeTimeSeconds) {
            NSLog(@"Time Between Open and Close Time %lu",(unsigned long)currentTimeSeconds);
        }

它显示0个开胃菜。 请帮帮我一个? 提前谢谢

1 个答案:

答案 0 :(得分:0)

如果可能的话,我会考虑使用libs / frameworks通过他们的API实现这一目标。例如,我使用DateTools

完成了日期操作