16
votes

Im trying to fetch all the objects in an entity matching a user selectedDate (it's an NSDate). The Core Data code is fine but my predicate keeps returning 0 results, the dates are the same in the database as the user is selecting.

How should the selectedDate be compared with a date from an entity using a predicate?

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(eDate = %@)", selectedDate];
5

5 Answers

25
votes

Your predicate looks to be fine.

The reason you're finding zero result are returned however may be that the dates aren't entirely the same.

For example:

05/04/2012 13:37:00 will not match with 05/04/2012 13:37:01 as these two values are not exactly the same.

Do you want to check the date (day, month, year) as well as the time?

If you only want to check the date, you should create a start date and end date and compare them using the user selected date as a frame of reference.

Something similar to this should create a date and time for 00:00:00.

//gather current calendar
NSCalendar *calendar = [NSCalendar currentCalendar];

//gather date components from date
NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:[NSDate date]];

//set date components
[dateComponents setHour:0];
[dateComponents setMinute:0];
[dateComponents setSecond:0];

//return date relative from date
return [calendar dateFromComponents:dateComponents];

Create another date by setting the hours, minutes and seconds to 23:59:59 and check that your user selected date falls between these ranges.

[NSPredicate predicateWithFormat:@"date BETWEEN %@", [NSArray arrayWithObjects:startOfDay, endOfDay, nil]]
12
votes

While in human communication date often is equal with day, it is not the same with NSDate objects: A NSDate represents a single moment in time. Dates that are different just in seconds aren't equal. And actually they dont represent days, month, year at all, as this is different from calendar system to calendar system

you must define for yourself if dates in the same minute, hour, day … are equal. So basically you must teach the program to allow some fuzziness.

here for minute resolution

NSDate *startDate = ....;
NSTimeInterval length;

[[NSCalendar currentCalendar] rangeOfUnit:NSMinuteCalendarUnit 
                                startDate:&startDate
                                 interval:&length 
                                  forDate:startDate];

NSDate *endDate = [startDate dateByAddingTimeInterval:length];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(eDate >= %@) AND (eDate < %@)", startDate, endDate];

For dates on the same day (aware of Timezones and Daylight Saving Times) you just would change this:

[[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit 
                                startDate:&startDate
                                 interval:&length 
                                  forDate:startDate];
2
votes

I can get working with some modifications to the accepted answer and using the iOS 8 API for create dates with time offset. The code:

NSCalendar *calendar = [[FFSharedCalendar singleton] getGregorianCalendar];
NSDate *startOfDay = [calendar dateBySettingHour:0 minute:0 second:0 ofDate:[NSDate date] options:0];
NSDate *endOfDay = [calendar dateBySettingHour:23 minute:59 second:59 ofDate:[NSDate date] options:0];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"createdAt > %@ AND createdAt < %@", startOfDay, endOfDay];
NSArray* plans = [Plan MR_findAllWithPredicate:predicate];

Hope it helps someone

0
votes

I've recently spent some time attempting to solve this same problem and have resolved the following, now updated for iOS 8 and above...

NSDate *dateDay = nil;
NSDate *dateDayStart = nil;
NSDate *dateDayNext = nil;

dateDay = <<USER_INPUT>>;  //for example - selectedDate

dateDayStart = [[NSCalendar currentCalendar] startOfDayForDate:dateDay];

//  dateDayNext EITHER
dateDayNext = [dateDayStart dateByAddingTimeInterval:(24 * 60 * 60)];

//  dateDayNext OR
NSDateComponents *dateComponentDay = nil;
dateComponentDay = [[NSDateComponents alloc] init];
[dateComponentDay setDay:1];
dateDayNext = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponentDay
                                                            toDate:dateDayStart
                                                           options:NSCalendarMatchNextTime];

...and the NSPredicate for the Core Data NSFetchRequest (as already shown above in other answers)...

[NSPredicate predicateWithFormat:@"(dateAttribute >= %@) AND (dateAttribute < %@)", dateDayStart, dateDayNext]]
0
votes

I query in between 1 second above and below my actual Date. In my case its ok to add and subtract 1 second as I query for the restaurant orders so I know that it takes some time to punch a new order, for safe side I have 1 more orderNo field too.

    let fetchRequest: NSFetchRequest = Order.fetchRequest()
    let date_formatter = DateFormatter()
    date_formatter.dateFormat = "yyyy-MM-dd-HH-mm-ss"
    date_formatter.timeZone = TimeZone(abbreviation: "UTC")

    let createdDate = date_formatter.date(from: clientCreatedStr)

    let calendar = NSCalendar.current

    //as matching exact same datetime doesnt return anything
    let onesecondafter = calendar.date(byAdding: .second, value: 1, to: createdDate!)
    let onesecondbefore = calendar.date(byAdding: .second, value: -1, to: createdDate!)

    let predicate = NSPredicate(format: "offlineUniqueId == %@ AND (clientCreatedDate >= %@ AND clientCreatedDate <= %@)", offlineUniqueId, onesecondbefore! as NSDate, onesecondafter! as NSDate)

    fetchRequest.predicate = predicate