Deviks
Deviks

Reputation: 13

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

Upvotes: 0

Views: 1816

Answers (2)

mvadim
mvadim

Reputation: 152

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

Upvotes: 4

vikingosegundo
vikingosegundo

Reputation: 52237

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.

Upvotes: 2

Related Questions