Search code examples
iosobjective-cnsdate

Open Hours in Objective-C - Time between two times NSDate


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.

Solution

  • 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];
    }