7

In my model I have date attribute and I set it [NSDate date], but getting with predicate like

 NSPredicate *predicate = [NSPredicate predicateWithFormat:
                              @"date==%@ ,date]; 

returns notting. I know the problem is when I set [NSDate date] it stores also time and NSPredicate always return empty data. Is there any way to convert "date==%@" part to only look for date?

Ercan
  • 2,699
  • 10
  • 53
  • 60
  • 1
    The problem with doing predicates with dates like this is that this will be doing a sub-millisecond comparison; you must have *exactly* the same instant in time, not just the same calendar day. – Dave DeLong Sep 19 '12 at 22:49
  • This "you must have exactly the same instant in time" made me think of this idea to deal with time in dates: make the time of your date all 0s right before storing it. And when retrieving from model pass a date value with time as 0s. – Only You Nov 04 '12 at 19:44

1 Answers1

20
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(date >= %@) AND (date <= %@)", startDate, endDate];
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
cancerian
  • 942
  • 1
  • 10
  • 18
  • thanks for response , but I have one date (today). endDate should be tomorrow? – Ercan Sep 19 '12 at 17:48
  • its ur choice.. don't give end date, remove tat part. pass only startDate. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(date >= %@)", startDate]; – cancerian Sep 20 '12 at 05:23
  • setting only startDate caused that if I give 2 days before date it returns me all records between 2 days before and today. I fixed it adding 1 day to selected date and used your first response with endDate. – Ercan Sep 20 '12 at 12:12
  • Are the brackets really necessary? – Rambatino Sep 08 '14 at 01:23