检查时间是否在两个时间之间 iOS

Check whether time falls between two time iOS

我有两次,比如说早上 8 点 40 分和下午 4 点, 我想做的是,想检查当前时间是否在给定时间之间?

我试过这个代码片段,但它不起作用:( 你能帮我解决我犯的错误吗?

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]];
    NSInteger currHr = [components hour];
    NSInteger currtMin = [components minute];

    NSString startTime = @"21:00";
    NSString endTime = @"07:00";
NSArray *arr=[NSArray arrayWithObjects:startTime,endTime, nil];

    int stHr = [[[[arr objectAtIndex:0] componentsSeparatedByString:@":"] objectAtIndex:0] intValue];
    int stMin = [[[[arr objectAtIndex:0] componentsSeparatedByString:@":"] objectAtIndex:1] intValue];
    int enHr = [[[[arr objectAtIndex:1] componentsSeparatedByString:@":"] objectAtIndex:0] intValue];
    int enMin = [[[[arr objectAtIndex:1] componentsSeparatedByString:@":"] objectAtIndex:1] intValue];

    int formStTime = (stHr*60)+stMin;
    int formEnTime = (enHr*60)+enMin;

    int nowTime = (currHr*60)+currtMin;

    if(nowTime >= formStTime && nowTime <= formEnTime) {
        NSLog(@"Btween......");
    }

提前致谢

编辑:

NSDateComponents *openingTime = [[NSDateComponents alloc] init];
    openingTime.hour = [timeA integerValue]; //8
    openingTime.minute = [timeB integerValue];  //45

    NSDateComponents *closingTime = [[NSDateComponents alloc] init];
    closingTime.hour = [timeC integerValue]; //4
    closingTime.minute = [timeD integerValue];  //43


    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        [formatter setDateFormat:@"hh:mm"];

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

    NSDate *now = [formatter dateFromString:nowTimeString]; //3:30

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

试试这个 -

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

查看截图 -

  • 为开始和结束时间创建日期组件。
  • 从要检查的日期创建具有小时、分钟、秒的日期组件
  • 将开盘、收盘和当前时间放在一个数组中
  • 排序数组。如果当前时间在索引 1 处,则它位于开盘时间和收盘时间之间

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