确定 NSPredicate 中的 NSDate 是否是今天

发布于 2024-09-07 15:38:56 字数 128 浏览 2 评论 0原文

我尝试了多种方法并尝试了此网站上的一些建议,但是如何确定 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 技术交流群。

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

发布评论

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

评论(6

还不是爱你 2024-09-14 15:38:56

这就是我想到的:

- (NSPredicate *)matchAttribute:(NSString *)attributeName withDate:(NSDate *)date
{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit fromDate:date];
    NSDateComponents *oneDay = [[NSDateComponents alloc] init];
    oneDay.day = 1;

    NSDate *lastMidnight = [[NSCalendar currentCalendar] dateFromComponents:components];
    NSDate *nextMidnight = [[NSCalendar currentCalendar] dateByAddingComponents:oneDay toDate:lastMidnight options:NSWrapCalendarComponents];

    return [NSPredicate predicateWithFormat:@"(%K >= %@) AND (%K <= %@)", attributeName, lastMidnight, attributeName, nextMidnight];
}

灵感来自 此处此处此处

This is what I came up with:

- (NSPredicate *)matchAttribute:(NSString *)attributeName withDate:(NSDate *)date
{
    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit fromDate:date];
    NSDateComponents *oneDay = [[NSDateComponents alloc] init];
    oneDay.day = 1;

    NSDate *lastMidnight = [[NSCalendar currentCalendar] dateFromComponents:components];
    NSDate *nextMidnight = [[NSCalendar currentCalendar] dateByAddingComponents:oneDay toDate:lastMidnight options:NSWrapCalendarComponents];

    return [NSPredicate predicateWithFormat:@"(%K >= %@) AND (%K <= %@)", attributeName, lastMidnight, attributeName, nextMidnight];
}

Inspiration from here, here and here.

新雨望断虹 2024-09-14 15:38:56

通过使用 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.

倾城°AllureLove 2024-09-14 15:38:56

今天的概念很棘手,因为它是什么意思?这就是为什么它并不简单。您的意思是在同一年、同一月、同一年,无论时区如何?你的意思是在同一个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.

鸠魁 2024-09-14 15:38:56

快速回答

我使用 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() and dateAtEndOfDay()

Then its a breeze.

NSPredicate(format: "pickup_time > %@ && pickup_time < %@",NSDate().dateAtStartOfDay(),NSDate().dateAtEndOfDay())

樱桃奶球 2024-09-14 15:38:56

您可能想尝试的一些事情。

  1. 添加另一个整数属性 dueDateYYYMMDD 到您的实体。使用 NSDateFormatter 将 NSDate 转换为 YYYYMMDD 字符串,然后使用 NSString integerValue 生成 dueDateYYYYMMDD。
  2. 在 [NSDate date] 上使用 NSDateFormatter 以 YYYYMMDD 格式生成“今天”,并使用 [NSString integerValue] 获取整数形式的“今天”(todayInt)。现在可以轻松构建谓词来比较 dueDateYYYYMMDD == TodayInt。

整数值的比较比字符串更有效。

Couple things that you might want to try.

  1. Add another integer attribute, dueDateYYYMMDD to your entity. Convert the NSDate using NSDateFormatter to the YYYYMMDD string, then use NSString integerValue to produce the dueDateYYYYMMDD.
  2. Use NSDateFormatter on [NSDate date] to produce "today" in YYYYMMDD format and [NSString integerValue] to obtain "today" as an integer (todayInt). Now your predicate can be easily constructed to compare dueDateYYYYMMDD == todayInt.

Comparison on integer values is more efficient than strings.

甜心 2024-09-14 15:38:56

您可能会发现 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

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