我想通过与当前时间的比较来从数组中找到最近的时间。(即,如果当前时间是上午 10:00,在这种情况下,有一个数组,它有 9:00am、9:50am、9 :30 上午。 由此我想得到上午 9:50。我怎样才能做到这一点?我不想更改数组中对象的索引值。
最佳答案
解析数组,将每个值与当前时间进行比较,存储数组中的时间与当前时间之间的秒数增量以及数组中值的索引。检查下一个是否具有较小的时间增量,并且是 override 值和索引。在解析结束时,您将获得最接近的时间戳和时间增量的索引。
正在做
NSTimeInterval interval = 3600*24; // just a big number
NSUInteger indexOfDate;
// I didnt checked if the delta can be negative in this case think aboud absolute number
for (NSDate * date in dateArray) {
if(abs([date timeIntervalSinceDate:[NSDate date]]) < interval) {
interval = abs([date timeIntervalSinceDate:[NSDate date]]);
indexOfDate = [dateArray indexOfObject:date];
}
}
关于iphone - 需要根据当前时间找到最近的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10752446/