我正在一个项目中,我需要确定所选的时隙是否在另一个时隙之间。例如,有一个像1:00 AM到2:00 AM这样的时隙现在我想知道这个时隙是否在12:45 AM到3:45 AM之间?
答案 0 :(得分:2)
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"hh:mm a"];
NSDate *generatedStartDate = [formatter dateFromString:@"01:00 AM"];
NSDate *generatedEndDate = [formatter dateFromString:@"03:44 AM"];
NSDate *slotStartDate = [formatter dateFromString:@"01:00 AM"];
NSDate *slotEndDate = [formatter dateFromString:@"03:45 AM"];
NSComparisonResult result = [slotStartDate compare:generatedStartDate];
NSComparisonResult result1 = [generatedEndDate compare:slotEndDate];
if (result != NSOrderedDescending && result1 != NSOrderedDescending) {
NSLog(@"IN Between");
} else {
NSLog(@"NOT IN Between");
}