简体   繁体   English

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

[英]how to calculate difference between two times in different dates in iOS?

starttime= 23:00 pm end time= 5:00 am 开始时间= 23:00 pm结束时间= 5:00 am

I need the summing time of the start time to end time i am trying this code but it is not helpful to me 我正在尝试这段代码,需要从开始时间到结束时间的总和,但这对我没有帮助

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);
        }

and it shows 0 hors. 它显示0小时。 please help me some one? 请帮我一个吗? thanks in advance 提前致谢

If possible, I'd look at using libs/frameworks to accomplish that through their APIs. 如果可能的话,我将研究使用libs / frameworks通过它们的API来实现这一点。 For example, I've done date operations using DateTools 例如,我已经使用DateTools完成了日期操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM