需要检查当前时间间隔是否位于目标 c 中的开始时间和结束时间之间

发布于 2024-11-16 23:47:15 字数 219 浏览 3 评论 0原文

我想知道当前时间是否存在于开始时间和输入之间,两者(开始时间和结束时间)都是静态的。像这样假设我当前时间是上午 10:15,开始时间是上午 9:15,结束时间是上午 11:00。所以这应该是函数应该返回一个 BOOL 值,在这种情况下是 yes,现在假设我当前时间是 12:00 AM 那么函数应该返回一个 bool 值 NO。所以我对如何创建这样一个函数(方法)来实现上述技巧感到困惑。请注意,开始时间和结束时间为同一天。

I want to know whether the current time is present between the start time and the enter, both (start-time and end-time) are static. Something like this suppose my current time is 10:15 AM the start time is 9:15AM and End time is 11:00 AM. So this should be the function should return me A BOOL value that is in this case is yes, now suppose my current time is 12:00 AM then the function should return me a bool value NO. So I am in confusion about how to create such a function (method) which will do the above trick. Note the start time and end time will be for the same day.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生寂 2024-11-23 23:47:15

NSDate 具有执行此操作所需的所有功能。查看文档。

NSDate has all the functions you need to do this. Check out the documentation.

昔梦 2024-11-23 23:47:15
  NSDate * now = [NSDate date];
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
    [outputFormatter setDateFormat:@"HH:mm:ss"];

  //current time
    NSString *currentTimeString = [outputFormatter stringFromDate:now];
    NSDate *dateCurrent = [outputFormatter dateFromString:currentTimeString];


    NSString *timeStart = @"09:00:00";
    NSString *timeEnd = @"22:00:00";

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

    NSDate *dateStart= [formatter timeStart];
    NSDate *dateEnd = [formatter timeEnd];
    NSComparisonResult result = [dateCurrent compare:dateStart];
    NSComparisonResult resultSecond = [date2 compare:dateEnd];

  if(result == NSOrderedDescending && resultSecond == NSOrderedDescending)
    {
        NSLog(@"current time lies in starting and end time");

}else {
        NSLog(@"current time don't lie in starting and end time");

    }
  NSDate * now = [NSDate date];
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
    [outputFormatter setDateFormat:@"HH:mm:ss"];

  //current time
    NSString *currentTimeString = [outputFormatter stringFromDate:now];
    NSDate *dateCurrent = [outputFormatter dateFromString:currentTimeString];


    NSString *timeStart = @"09:00:00";
    NSString *timeEnd = @"22:00:00";

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

    NSDate *dateStart= [formatter timeStart];
    NSDate *dateEnd = [formatter timeEnd];
    NSComparisonResult result = [dateCurrent compare:dateStart];
    NSComparisonResult resultSecond = [date2 compare:dateEnd];

  if(result == NSOrderedDescending && resultSecond == NSOrderedDescending)
    {
        NSLog(@"current time lies in starting and end time");

}else {
        NSLog(@"current time don't lie in starting and end time");

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文