Objective-C中两次之间的比较 [英] Comparison between two times in objective-c
本文介绍了Objective-C中两次之间的比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的数组的时间如下所示
I have array with times like below
"00:00",
"01:25",
"02:00",
"02:35",
"04:35",
"05:00",
"06:00",
"07:00",
"09:00",
"16:30",
"17:30",
"18:00",
"18:30",
"19:30",
"21:30",
"22:00",
"22:30",
"23:00"
如何获取接近当前时间的对象的索引?
how get index of object which time near to current time?
推荐答案
您可以获得这样的时差
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:(NSDayCalendarUnit | NSHourCalendarUnit | NSMinCalendarUnit) fromDate:today];
[components setHour:otherHour];
[components setMinute:otherMinute];
NSDate *otherDate = [gregorian dateFromComponents:components];
NSTimeInterval timeDifferenceBetweenDates = [today timeIntervalSinceDate:otherDate];
在所有小时/分钟内执行此操作,并采用最小的绝对时间间隔.
do that for all your hours/minutes and take the minimum absolute time interval.
这篇关于Objective-C中两次之间的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文