我有如下所示的时间数组
"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"
如何获取当前时间附近对象的索引?
最佳答案
这样可以得到时差
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];
在你所有的小时/分钟内都这样做,并采用最小的绝对时间间隔。
关于iphone - objective-c中两次比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8034452/