使用 NSOrderedAscending 和 NSOrderedDescending 指定特定范围内的日期
我有这个代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确定当firstDate等于dateSelected时选择的日期早于secondDate吗?目前您的逻辑是:firstDate <= dateSelected <第二个日期。如果你想要firstDate <= dateSelected <= secondaryDate,那么你需要实现代码如下:
更新:
要比较两个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:
Update:
To compare two NSDates without relying on time see: Comparing two NSDates and ignoring the time component