两次之间有if
语句的最简单方法是什么?例如:
if ([timeisBetween=1:00 and 8:40]) {
NSLog(@"Inside time period");
}
else {
NSLog(@"Not in time period - after 8:30 in the morning")
}
和
if ([timeisBetween=22:00 and 5:40]) {
NSLog(@"Inside time period 10:00 at night to 5:40 in the morning");
}
else {
NSLog(@"Not in time period");
}
答案 0 :(得分:1)
通过NSDateFormatter将NSStrings转换为NSDates,然后进行比较。确保您的NSDate介于这些范围之间。这些片段将帮助您指明正确的方向。
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *startDate = [formatter dateFromString:@"2014-17-03 6:45:58"];
NSDate *endDate = [formatter dateFromString:@"2014-17-03 8:45:58"];
if ([startDate compare: endDate] == NSOrderedDescending) {
NSLog(@"start is later than end");
} else if ([startDate compare:endDate] == NSOrderedAscending) {
NSLog(@"start is earlier than end");
} else {
NSLog(@"Same Dates");
}