使用 NSOrderedAscending 和 NSOrderedDescending 指定特定范围内的日期

发布于 2024-11-14 04:30:19 字数 404 浏览 3 评论 0原文

我有这个代码:

NSComparisonResult compareStart = [firstDate compare: dateSelected]; 
NSComparisonResult compareEnd = [secondDate compare: dateSelected];

if (((compareStart == NSOrderedAscending) || (compareStart == NSOrderedSame))
                   && (compareEnd == NSOrderedDescending)) 

但是当firstDate = dateSelected或secondDate = dateSelected时它不会进入“if”...为什么?

I have this code:

NSComparisonResult compareStart = [firstDate compare: dateSelected]; 
NSComparisonResult compareEnd = [secondDate compare: dateSelected];

if (((compareStart == NSOrderedAscending) || (compareStart == NSOrderedSame))
                   && (compareEnd == NSOrderedDescending)) 

but it don't entry in the "if" when firstDate = dateSelected or secondDate = dateSelected...why??

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

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

发布评论

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

评论(1

Saygoodbye 2024-11-21 04:30:19

您确定当firstDate等于dateSelected时选择的日期早于secondDate吗?目前您的逻辑是:firstDate <= dateSelected <第二个日期。如果你想要firstDate <= dateSelected <= secondaryDate,那么你需要实现代码如下:

NSComparisonResult compareStart = [firstDate compare: dateSelected]; 
NSComparisonResult compareEnd = [secondDate compare: dateSelected];

if ( (compareStart == NSOrderedAscending || compareStart == NSOrderedSame)
                   && (compareEnd == NSOrderedDescending || compareEnd == NSOrderedSame)) {
  // some code
}

更新:

要比较两个NSDate而不依赖于时间,请参阅:比较两个 NSDate 并忽略时间部分

Are you sure that when firstDate equals dateSelected that date selected is earlier than secondDate? Currently your logic is: firstDate <= dateSelected < secondDate. If you want firstDate <= dateSelected <= secondDate, then you need to implement the code as follows:

NSComparisonResult compareStart = [firstDate compare: dateSelected]; 
NSComparisonResult compareEnd = [secondDate compare: dateSelected];

if ( (compareStart == NSOrderedAscending || compareStart == NSOrderedSame)
                   && (compareEnd == NSOrderedDescending || compareEnd == NSOrderedSame)) {
  // some code
}

Update:

To compare two NSDates without relying on time see: Comparing two NSDates and ignoring the time component

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