【问题标题】:Need to check if the current time interval lies between start and end time in objective c需要检查当前时间间隔是否位于目标 c 的开始时间和结束时间之间
【发布时间】:2011-06-24 07:21:19
【问题描述】:

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

【问题讨论】:

  • 寻找答案here
  • 嗨@Mrugen,你有办法缩短这个吗?提前感谢您的帮助。

标签: iphone xcode nsdate


【解决方案1】:

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

【讨论】:

  • 我搜索了很多但找不到一个如果你能告诉我这个功能或者一些链接会有帮助会很棒。我真的需要它因为应用程序处于最后阶段并且只有这个事情就剩下了。如果你能帮助我(快),那就太好了。
  • 您可以通过转到 xCode 中的管理器并单击“文档”按钮找到有关所有 Apple 功能的大量且非常有用的文档。在那里搜索 NSDate。
  • 使用 compare: 函数将样本日期与开始日期和结束日期进行比较。
  • 如果您认为值得,请随时给答案投赞成票并接受。
【解决方案2】:
  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");

    }

【讨论】:

    猜你喜欢
    • 2013-01-06
    • 2011-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    相关资源
    最近更新 更多