检查时间是否落在两次iOS之间 [英] Check whether time falls between two time iOS

查看:111
本文介绍了检查时间是否落在两次iOS之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 but it is not working :( can you please help me out where i am making mistake ?

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

提前Thnaks >

编辑

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处,则处于开始和结束时间之间

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

      这篇关于检查时间是否落在两次iOS之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆