Check whether time falls between two time iOS

前端 未结 2 1629
终归单人心
终归单人心 2020-12-11 11:45

I have two times let say 8.40am and 4.00pm, What i want to do is, want to check whether current time falls between given time or not ?

I have tried this code snippet

相关标签:
2条回答
  • 2020-12-11 12:27

    Try this -

    NSString *startTimeString = @"08:00 AM";
    NSString *endTimeString = @"06:00 PM";
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"hh:mm a"];
    
    NSString *nowTimeString = [formatter stringFromDate:[NSDate date]];
    
    int startTime   = [self minutesSinceMidnight:[formatter dateFromString:startTimeString]];
    int endTime  = [self minutesSinceMidnight:[formatter dateFromString:endTimeString]];
    int nowTime     = [self minutesSinceMidnight:[formatter dateFromString:nowTimeString]];;
    
    
    if (startTime <= nowTime && nowTime <= endTime)
    {
        NSLog(@"Time is between");
    }
    else {
        NSLog(@"Time is not between");
    }
    
    
    -(int) minutesSinceMidnight:(NSDate *)date
    {
        NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:date];
        return 60 * (int)[components hour] + (int)[components minute];        
    }
    

    Check the screenshot -

    enter image description here

    0 讨论(0)
  • 2020-12-11 12:39
    • create date components for opening and closing time.
    • create date components with hour, minute, second from date to check
    • place opening, closing and current time in an array
    • sort array. if current time is at index 1, it lies between opening and closing time

    NSDateComponents *openingTime = [[NSDateComponents alloc] init];
    openingTime.hour = 8;
    openingTime.minute = 40;
    
    NSDateComponents *closingTime = [[NSDateComponents alloc] init];
    closingTime.hour = 16;
    closingTime.minute = 0;
    
    NSDate *now = [NSDate date];
    
    NSDateComponents *currentTime = [[NSCalendar currentCalendar] components:NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond
                                                                    fromDate:now];
    
    NSMutableArray *times = [@[openingTime, closingTime, currentTime] mutableCopy];
    [times sortUsingComparator:^NSComparisonResult(NSDateComponents *t1, NSDateComponents *t2) {
        if (t1.hour > t2.hour) {
            return NSOrderedDescending;
        }
    
        if (t1.hour < t2.hour) {
            return NSOrderedAscending;
        }
        // hour is the same
        if (t1.minute > t2.minute) {
            return NSOrderedDescending;
        }
    
        if (t1.minute < t2.minute) {
            return NSOrderedAscending;
        }
        // hour and minute are the same
        if (t1.second > t2.second) {
            return NSOrderedDescending;
        }
    
        if (t1.second < t2.second) {
            return NSOrderedAscending;
        }
        return NSOrderedSame;
    
    }];
    
    if ([times indexOfObject:currentTime] == 1) {
        NSLog(@"We are Open!");
    } else {
        NSLog(@"Sorry, we are closed!");
    }
    
    0 讨论(0)
提交回复
热议问题