Determining if an NSDate is today in NSPredicate

大城市里の小女人 提交于 2020-01-02 07:12:13

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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())




回答5:


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.




回答6:


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



来源:https://stackoverflow.com/questions/3168374/determining-if-an-nsdate-is-today-in-nspredicate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!