我有开始时间和结束时间,格式为 HH:mm:ss(24 小时制)。
我需要比较当前时间是否在开始时间和结束时间之间。
我以这样一种方式编写逻辑代码,如果开始时间晚于结束时间,它将在开始时间上增加 24 小时。
if (shift.sShiftStart < shift.sShiftEnd) {
startTime = shift.sShiftStart;
startTimeInt = [[shift.sShiftStart substringToIndex:2] integerValue];
startTimeInt2 = startTimeInt + 24;
finalStartTime = [startTime stringByReplacingCharactersInRange:NSMakeRange(0, 2) withString:[NSString stringWithFormat:@"%d", startTimeInt2]];
} else {
finalStartTime = shift.sShiftStart;
}
现在我想检查我的 currentTime 是否在 finalStartTime 和 endTime 之间(都是 HH:mm:ss 格式)
最佳答案
将字符串转换为日期 ( NSDate
) 并使用 compare:
方法。不要假设你可以使用 <
在对象实例上,他们会神奇地知道你想让他们用它做什么(你实际上是在比较指针值)。
一旦你有了日期,你就可以添加时间(另请参阅 NSDateComponents
类)。
关于ios - 比较时间(格式为 HH :MM:SS) if between two times in IOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19997018/