iPhone - 比较日期未按预期工作
我有一个设置为特定日期的活动。我想检查日期是否在当前日期的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试以下代码。
Try the following code.
我认为你应该在计算中切换 eventDate 和 currentDate 。
I think you should switch around eventDate and currentDate in your calculation.
我认为 EmptyStack 的解决方案是解决这个问题的最佳方案,但我只是将其简化如下:
我对此很好奇......所以我编写了一个快速测试,一切都按我的预期进行。给出下面的示例代码,在第一次运行中,与现在相比,正五是 NSOrderedAscending ,正如预期的那样,负五是 NSOrderedDescending 。在第二次运行中,与三分钟后的日期相比,加五是 NSOrderedAscending,减五是 NSOrderedDescending。最后,与二十分钟后的日期相比,加五是 NSOrderedDescending ,减五也是 NSOrderedDescending ,一切都符合预期。
I think EmptyStack's solution is the best for this problem, but I'd just simplify it as follows:
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 isNSOrderedDescending
. In the second run, plus five isNSOrderedAscending
and minus five isNSOrderedDescending
when compared with a date three minutes from now. Finally, plus five isNSOrderedDescending
and minus five is alsoNSOrderedDescending
when compared with a date twenty minutes from now, again, all as expected.