Determining if an NSDate is today in NSPredicate
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 da开发者_运维问答y/month/year regardless of what the time is in the day. Any suggestions?
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.
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.
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.
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())
Couple things that you might want to try.
- 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.
- 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.
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
精彩评论