-2

What is the easiest way to have an if statement between two times? For example:

if ([timeisBetween=1:00 and 8:40]) {
    NSLog(@"Inside time period");
}
else {
    NSLog(@"Not in time period - after 8:30 in the morning")
}

and

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");
}
Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Seb OH
  • 785
  • 1
  • 6
  • 15
  • 2
    What have you tried so far? What data do you have and what is its type (`NSDate`, `NSTimeInterval`, `NSString`)? – rmaddy Mar 17 '14 at 22:40
  • 2
    There are several ways to do it, but they all require actual programming, if you want a reasonably general solution. – Hot Licks Mar 17 '14 at 22:50
  • possible duplicate of [How to Check if an NSDate occurs between two other NSDates](http://stackoverflow.com/questions/1072848/how-to-check-if-an-nsdate-occurs-between-two-other-nsdates) – jscs Mar 17 '14 at 23:57

1 Answers1

1

Convert your NSStrings to NSDates via a NSDateFormatter and then compare them. Make sure your NSDate is between those ranges. These snippets will help point you in the right direction.

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");

}
PCoder123
  • 364
  • 2
  • 11
  • Thanks for the answer. What do I do with times of everyday, rather than a set date?? Thanks, – Seb OH Mar 19 '14 at 00:46