This is a little more complicated then just
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"startDate >= %@ AND endDate <= %@", startDay,endDay];
I’m building a calendar app and I need to pull events out of my core data store that occur on a given day. However, it’s possible for an event to start/end on a different day then the day I’m trying to display, e.g.
My Date Range (Start = 2011-12-02 00:00:00 to End = 2011-12-02 23:59:59)
An Event (Start = 2011-12-01 23:00:00 to End = 2011-12-02 05:00:00)
How can I write a predicate to determine if that event falls in that date range. Keep in mind that an event could start before and after the date range.
Thanks!
Here is a method to build a predicate to retrieve non recurring events occurring on a given day (recurring events require additional processing):
The predicate is almost equal to the one proposed by @Tony, except it does not check for equality. Here is why. Suppose you have an event starting on December 8 23:00 and ending at December 9 00:00. If you check for events whose ending date is >= rather than > of the given day, your app will report the event in both December 8 and 9, which is clearly wrong. Try adding such an event to both iCal and Google Calendar, and you will see that the event only appears on December 8. In practice, you should not assume that a given day ends at 23:59:59 (even though this is of course true): you need to treat midnight as the last second of a given day (with regard to the end of an event). Also, note that this does not prevent events starting at midnight.