0

I am trying to perform a segue in Objective-C (XCode) for iOS devices, when the time right now is between two other fixed times. Just like "Open Hours" for stores - when the time right now is between open and close hour.

Here is the code I have been working on - some of the code may look familiar, because I found some useful stuff on SO which helped me - but still I can't get it to work. It doesn't perform the segue when the time passes startTime. It should be in the specified time interval.

The time is in 24-hour format.

 // set start time and end time
NSString *startTimeString = @"23:00";
NSString *endTimeString = @"05:00";

// set date formatter 24-hour format
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"HH:mm"];

// german timezone
NSLocale *locale = [[NSLocale alloc]initWithLocaleIdentifier:@"de_DE"];


NSString *nowTimeString = [formatter stringFromDate:[NSDate date]];

// set NSDates for startTime, endTime and nowTime
NSDate *startTime   = [formatter dateFromString:startTimeString];
NSDate *endTime  = [formatter dateFromString:endTimeString];
NSDate *nowTime     = [formatter dateFromString:nowTimeString];

[formatter setLocale:locale];

// compare endTime and startTime with nowTime
NSComparisonResult result1 = [nowTime compare:endTime];
NSComparisonResult result2 = [nowTime compare:startTime];

if ((result1 == NSOrderedDescending) &&
    (result2 == NSOrderedAscending)){

    NSLog(@"Time is between");
    [self performSegueWithIdentifier:@"openHours" sender:self];

} else {
    NSLog(@"Time is not between");
}
  • thanks for taking your time to look at my question. I have been searching and searching, trying and trying, but no luck in making it work yet. Hopefully your answers will help me.
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Deviks
  • 13
  • 2
  • Add some NSLogs of your intermediate values so you understand what's going on. Since you don't specify a day, the NSDate values will be Jan 1 of some year (2000?). Also note that your start time is later than your end time, presumably meaning that your "store" opens before midnight and remains open until 5 am. If that is your intent you may need to reverse the direction of the compares. – Hot Licks Jun 01 '14 at 14:23
  • 3
    (With something like this it's not generally good to copy code without understanding what it's doing.) – Hot Licks Jun 01 '14 at 14:24
  • Hi Hot Licks, yes. The year is 2000 and actually also dec. 31. 1999 - the nowTime spans over two days. Yes I tried to reverse the time span because I figured out that 05:00 is earlier than 23:00, but still no luck.. – Deviks Jun 01 '14 at 14:35
  • Is this what you are looking for http://stackoverflow.com/questions/20440333/compare-current-time-and-compare-it-with-two-nsstring-time-in-objective-c/20441330#20441330 ? – Martin R Jun 01 '14 at 14:40
  • And what does "no luck" mean? – Hot Licks Jun 01 '14 at 18:10
  • @HotLicks it means that it didn't work. The answer that mvadim gave is working for me right now. It fixes the two day span, which I got before because that solution only cares about how many minutes have passed since midnight. – Deviks Jun 01 '14 at 18:13

2 Answers2

4

You should little bit change you code

// set NSDates for startTime, endTime and nowTime
        int startTime   = [self minutesSinceMidnight:[formatter dateFromString:startTimeString]];
        int endTime  = [self minutesSinceMidnight:[formatter dateFromString:endTimeString]];
        int nowTime     = [self minutesSinceMidnight:[formatter dateFromString:nowTimeString]];;

        [formatter setLocale:locale];


if (nowTime < endTime && nowTime > startTime) {
    NSLog(@"Time is between");
} else if (nowTime > endTime && nowTime < startTime) {
    NSLog(@"Time is between");
} else {
    NSLog(@"Time is not between");
}

And implement method for calculating time:

-(int) minutesSinceMidnight:(NSDate *)date
{
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    unsigned unitFlags =  NSHourCalendarUnit | NSMinuteCalendarUnit;
    NSDateComponents *components = [gregorian components:unitFlags fromDate:date];

    return 60 * [components hour] + [components minute];
}
Pablo Marrufo
  • 855
  • 1
  • 8
  • 17
mvadim
  • 152
  • 6
  • Hi @mvadim, I changed the if-statement in your code, so it worked with this: `if (nowTime < endTime && nowTime < startTime) { NSLog(@"Time is between"); } else if (nowTime > endTime && nowTime > startTime) { NSLog(@"Time is between"); } else { NSLog(@"Time is not between"); }` It seems to work this way. Could you edit your answer so I can accept it? – Deviks Jun 01 '14 at 15:11
  • But now time should be more then start time and less end time... and you can use just else without expression. – mvadim Jun 01 '14 at 15:42
  • startTime is later than endTime because endTime is only 5 hours after midnight, while startTime is 23 hours after midnight. – Deviks Jun 01 '14 at 17:16
2

I am interested in an answer that works on ANY date, and not fixed dates for opening and closing.

In that case the simplest approach would be using NSDateComponents. You could store hour, minutes and maybe weekday for opening and closing. to check for now you would break up [NSDate now] into the same NSDateComponents and cop are those.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178