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

时间:2022-12-13 03:01:16

starttime= 23:00 pm end time= 5:00 am

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

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. please help me some one? thanks in advance

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

1 个解决方案

#1


0  

If possible, I'd look at using libs/frameworks to accomplish that through their APIs. For example, I've done date operations using DateTools

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

#1


0  

If possible, I'd look at using libs/frameworks to accomplish that through their APIs. For example, I've done date operations using DateTools

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