【问题标题】:iPhone - comparing dates not working as expectediPhone - 比较日期未按预期工作
【发布时间】:2011-08-20 17:37:25
【问题描述】:

我有一个设置为特定日期的事件。我想检查日期是否在当前日期的 5 分钟范围内。例如:如果事件设置为晚上 9:10,而现在是晚上 9:02。我想看看当前时间是否在晚上 9:05 到晚上 9:15 之间。

所以,我愿意...

NSDate *currentDate ... this variable contains the current date
NSDate *plusFive = [eventDate dateByAddingTimeInterval:300];
NSDate *minusFive = [eventDate dateByAddingTimeInterval:-300];

NSComparisonResult result1 = [eventDate compare:minusFive];
NSComparisonResult result2 = [eventDate compare:plusFive];


if ( (result1 == NSOrderedAscending) && (result2 == NSOrderedDescending) ) { 
    // if the current date is not inside the 5 minutes window
    // or in other words, if the currentDate is lower than minusFive and bigger
    // than plusFive
    [self currentDateIsOutsideTheWindow];
} else {
    [self currentDateIsInsideTheWindow];
}

问题是 result1 和 result2 总是给我 NSOrderedAscending 作为结果,任何日期。

有什么线索吗?

谢谢。

【问题讨论】:

  • 你为什么用eventDate而不是currentDate
  • 我在我的答案中添加了一个快速代码示例......日期的compare: 方法似乎对我来说按预期工作,但我建议按照答案中的建议简化你的代码。

标签: iphone objective-c ios nsdate


【解决方案1】:

试试下面的代码。

NSTimeInterval ti = [eventDate timeIntervalSinceDate:currentDate];
if (ti > 0 && ti < 300) ...
else if (ti < 0 && ti > -300) ...

【讨论】:

    【解决方案2】:

    我认为您应该在计算中切换 eventDate 和 currentDate。

    【讨论】:

      【解决方案3】:

      我认为 EmptyStack 的解决方案最适合这个问题,但我只是简化如下:

      if( fabs([eventDate timeIntervalSinceNow]) <= 300.0 ) {
        // within the window
      } else {
        // outside the window
      }
      

      我对此很好奇......所以我写了一个快速测试,一切都按预期运行。给定下面的示例代码,在第一次运行中,与现在相比,正五是NSOrderedAscending,正如预期的那样,负五是NSOrderedDescending。在第二次运行中,与三分钟后的日期相比,加五是NSOrderedAscending,减五是NSOrderedDescending。最后,与二十分钟后的日期相比,加五是NSOrderedDescending,减五也是NSOrderedDescending,这一切都符合预期。

      // datetest.m
      // clang -o datetest datetest.m -framework foundation
      
      #import <Foundation/Foundation.h>
      
      void test_compare(NSDate* event_date)
      {
          NSDate* dateNow = [NSDate date];
      
          NSDate* minusFive = [event_date dateByAddingTimeInterval:-300.0];
          NSDate* plusFive = [event_date dateByAddingTimeInterval:300.0];
      
          NSComparisonResult minusFiveResult = [dateNow compare:minusFive];
          NSComparisonResult plusFiveResult = [dateNow compare:plusFive];
      
          NSLog(@"P5: %ld, M5: %ld; dates (now, event, p5, m5):\n\t%@\n\t%@\n\t%@\n\t%@", (long int)plusFiveResult, (long int)minusFiveResult, 
              dateNow, event_date, plusFive, minusFive);  
      }
      
      int main (int argc, char const *argv[])
      {
          NSAutoreleasePool *pool = [NSAutoreleasePool new];
      
          // test for an event that's "now"
          NSDate* now = [NSDate date];
          test_compare(now);
      
          // test for an event that's three minutes from now
          NSDate* threeMinutesLater = [now dateByAddingTimeInterval:180.0];
          test_compare(threeMinutesLater);
      
          // test for an event that's 20 minutes ago
          NSDate* twentyMinutesBefore = [now dateByAddingTimeInterval:-1200.0];
          test_compare(twentyMinutesBefore);
      
          [pool drain];
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-04
        • 2019-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-31
        • 2011-06-15
        • 1970-01-01
        相关资源
        最近更新 更多