ios - 检查时间是否介于两次iOS之间

标签 ios objective-c cocoa-touch nsdate nsdateformatter

我有两次说8.40am和4.00pm,
我想做的是,想要检查当前时间是否介于给定时间之间?

我已经尝试过此代码段,但它不起作用:(
你能帮我解决我犯错的地方吗?

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

最佳答案

  • 为打开和关闭时间创建日期组件。
  • 创建带有日期的小时,分​​钟,秒的日期组件以检查
  • 将打开,关闭和当前时间放置在一个数组
  • 排序数组。如果当前时间在索引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!");
    }
    

    关于ios - 检查时间是否介于两次iOS之间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29559944/

    相关文章:

    ios - sizeWithAttributes 返回带有新行字符的错误大小\n

    ios - 属性(property)还是非属性(property)?

    iphone - CGContextClearRect 访问错误

    ios - NS前缀是什么意思?

    iphone - 刷新 UITabBar 选项卡的 View 或在重新选择选项卡时从 UITabBar 选项卡的 View 调用方法

    ios - iOS 上的 Flex 4.6 TextInput 键盘

    ios - JSON 解析使用 [NSJSONSerialization JSONObjectWithData :dataJson options:0 error:&error] thowing nil

    ios - 如何构建用于 Facebook 提交的应用程序

    ios - 移动到下一个选项卡时 mapView 上的点消失

    objective-c - 调用[self setValue:forKey:]时应用崩溃