[英]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 提前致谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.