iPhone - 比较日期未按预期工作

发布于 2024-11-30 14:18:37 字数 897 浏览 1 评论 0原文

我有一个设置为特定日期的活动。我想检查日期是否在当前日期的 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 作为结果,对于任何日期。

有什么线索吗?

谢谢。

I have an event that is set to a particular date. I want to check if the date is within 5 minutes range of the current date. For example: if the event is set to 9:10 pm, and now is 9:02 pm. I want to see if the current time is between 9:05 and 9:15 pm.

So, I do...

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];
}

the problem is that result1 and result2 always gives me NSOrderedAscending as a result, for any date.

Any clues?

thanks.

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

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

发布评论

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

评论(3

晨光如昨 2024-12-07 14:18:37

尝试以下代码。

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

Try the following code.

NSTimeInterval ti = [eventDate timeIntervalSinceDate:currentDate];
if (ti > 0 && ti < 300) ...
else if (ti < 0 && ti > -300) ...
一瞬间的火花 2024-12-07 14:18:37

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

I think you should switch around eventDate and currentDate in your calculation.

听你说爱我 2024-12-07 14:18:37

我认为 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;
}

I think EmptyStack's solution is the best for this problem, but I'd just simplify it as follows:

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

I was curious about this... so I wrote a quick test and everything works as expected for me. Given the sample code below, in the first run, the plus five is NSOrderedAscending when compared with now, as expected, and the minus five is NSOrderedDescending. In the second run, plus five is NSOrderedAscending and minus five is NSOrderedDescending when compared with a date three minutes from now. Finally, plus five is NSOrderedDescending and minus five is also NSOrderedDescending when compared with a date twenty minutes from now, again, all as expected.

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