iphone - objective-c中两次比较

标签 iphone objective-c ios

我有如下所示的时间数组

         "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/

相关文章:

iphone - 无法将ipa上传到应用商店,表示应用缺少必需的体系结构。必须至少存在以下一种架构

ios - 加载 UIView 崩溃

objective-c - 检查一组坐标是否包含在形状中

cocoa - 尝试查看 .xcdatamodel 时 XCode 崩溃

ios - 如何解决 No + [RCTConvert AIR MapCoordinate Array] 错误 : function found. 在 iOS 上 react native map

iphone - 创建线条时 CGContextFillPath(context) 被删除

php - Json 返回 'null'

iphone - 核心显卡 CGContextSetFillColorWithColor 未填充我的形状

ios - Tesseract OCR 无法识别除法符号 "÷"

iphone - 动画 UIButton 背景图像