确定 NSPredicate 中的 NSDate 是否是今天
我尝试了多种方法并尝试了此网站上的一些建议,但是如何确定 CoreData 属性“dueDate”是否等于“今天”,我知道 NSDate 也是一个特定时间,但我希望谓词返回 if无论当天的时间是什么,都是同一天/同一月/同一年。有什么建议吗?
I've tried multiple methods and tried some suggestions on this site, but how can I determine if a CoreData attribute "dueDate" is equal to "Today", I know NSDate is a specific time as well but I want the predicate to return if it is the same day/month/year regardless of what the time is in the day. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这就是我想到的:
灵感来自 此处、此处和此处。
This is what I came up with:
Inspiration from here, here and here.
通过使用 NSDateComonents 和 NSCalendar 修复了这个问题。
刚刚从原始 NSDate 创建了仅日/月/年的 NSDateComponents,然后使用 dateFromComponents 将其重新创建为日期。
这导致它成为一天的开始,然后我使用上面的方法创建了另一个日期,即“明天”的开始并使用了 >且<看看是否是“今天”。不管怎样,谢谢你的帮助。
Fixed this by using NSDateComonents and NSCalendar.
Just created NSDateComponents of just day/month/year from the original NSDate and then re-created it to a date using dateFromComponents.
This lead to it being the start of the day, I then created another date using the method above that was the start of "tomorrow" and used > and < to see if it was "today." Thanks for the help anyway.
今天的概念很棘手,因为它是什么意思?这就是为什么它并不简单。您的意思是在同一年、同一月、同一年,无论时区如何?你的意思是在同一个24小时内吗?过去 24 小时内?它可能会变得复杂。
您对上述问题的回答决定了您问题的答案。如果是同一天、同一月、同一年,则使用 NSDateComponents 提取这三个值并进行比较。如果您的意思是 24 小时内,则可以使用日期方法
-timeIntervalSinceDate:
来确定日期之间的秒数。如果您有其他意思,请澄清。
The concept of today is tricky because what do you mean by it? That is why it is not straightforward. Do you mean as in the same day, month and year regardless of timezone? Do you mean within the same 24 hour period? Within the last 24 hours? It can get complicated.
Your answer to the above determines the answer to your question. If it is the same day, month and year then use NSDateComponents to extract those three values and compare them. If you mean within 24 hours then you can use the date method
-timeIntervalSinceDate:
to determine the number of seconds between the dates.If you mean something else then please clarify.
快速回答
我使用 AFDateHelper
如果您不想使用第三方。只需检查源代码即可找到
dateAtStartOfDay()
和dateAtEndOfDay()
的实现,然后就轻而易举了。
NSPredicate(格式: "pickup_time > %@ && Pickup_time < %@",NSDate().dateAtStartOfDay(),NSDate().dateAtEndOfDay())
Swift Answer
I use AFDateHelper
If you don't want to use third party. Just go check the source to find implementation of
dateAtStartOfDay()
anddateAtEndOfDay()
Then its a breeze.
NSPredicate(format: "pickup_time > %@ && pickup_time < %@",NSDate().dateAtStartOfDay(),NSDate().dateAtEndOfDay())
您可能想尝试的一些事情。
整数值的比较比字符串更有效。
Couple things that you might want to try.
Comparison on integer values is more efficient than strings.
您可能会发现 Erica Sadun NSDate 实用程序对此类事情很有帮助。
很多非常方便的零碎东西。一探究竟。
https://github.com/erica/NSDate-Extensions
马特
You might find Erica Sadun NSDate utilities helpful for this kind of thing.
Lot's of really handy bits and pieces. Check it out.
https://github.com/erica/NSDate-Extensions
Matt