Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data Predicate Date Comparison

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];
like image 375
user2228755 Avatar asked Apr 05 '13 10:04

user2228755


2 Answers

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]]
like image 116
Zack Brown Avatar answered Nov 05 '22 05:11

Zack Brown


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];
like image 12
vikingosegundo Avatar answered Nov 05 '22 07:11

vikingosegundo