Take the 2-minute tour ×

I have a Core Data entity which contains dates (e.g. 2011-03-30, 2011-06-30, 2011-09-30).

How would you recommend that I specify a NSPredicate to get the maximum date, which is below a specified date (2011-08-17), e.g. a query like date <= 2011-08-17 AND date = @max should return 2011-06-30 (which is the highest date but does not exceed the date given).

Could you please help me with the predicate?

Thank you!!

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You could use the simple less-than-or-equal-to predicate, set the order to descending and configure the fetch to only return a single result.

share|improve this answer
What would happen if there are two dates with the same date? Which one would be fetched, when the request returns only one result? –  AlexR Jul 7 '12 at 10:34
@AlexR The actual later date would be returned (the one with the later time). If two had exactly the same date/time, it would be undefined. You'd get one or the other. But since you're asking to return a single date, it shouldn't matter. If it does matter, you'd have to select on some other item as well as the date and order by that too. –  Jason Coco Jul 7 '12 at 17:39
add comment

Your Answer


