0
votes

i have a question... i have an entity in core data with a date attribute. I want my app to be able to check the data store if there exists a record with today's date so the user can either continue it or overwrite it. the problem is that I'm just fetching the requests using an NSPredicate like this:

self.whereClause = [NSPredicate predicateWithFormat:@"(date = %@)", [NSDate date]];

when i print out the dates to the console they show up

date: 2012-04-26 22:16:45 +0000

and the app is not able to recognize that there is an existing record.... i feel like it might be because of the time/timezone component.....

so how can i compare two NSDate objects retrieved from Core Data without the time components?

2

2 Answers

0
votes

You need to create and compare dates with 00:00 time [NSDate date] always contains the current time so a match for date only is not possible.

Take a look at NSDateComponents to create dates with the exact same time.

Another solution would be to create a query searching for (date > 2012-04-26 00:00:00 AND date < 2012-04-26 23:59:59) (Requires creating dates with time at beginning and end of day)

I would recommend the first solution if you want to search for a single exact match.

0
votes

Here's a link to a code snippet for removing the Time Component from NSDate, in case anyone was wondering

Removing time component from NSDate