Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6906185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:16:39+00:00 2026-05-27T08:16:39+00:00

This is a little more complicated then just NSPredicate *predicate = [NSPredicate predicateWithFormat:@startDate >=

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:16:39+00:00Added an answer on May 27, 2026 at 8:16 am

    Here is a method to build a predicate to retrieve non recurring events occurring on a given day (recurring events require additional processing):

    - (NSPredicate *) predicateToRetrieveEventsForDate:(NSDate *)aDate {
    
        // start by retrieving day, weekday, month and year components for the given day
        NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDateComponents *todayComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:aDate];
        NSInteger theDay = [todayComponents day];
        NSInteger theMonth = [todayComponents month];
        NSInteger theYear = [todayComponents year];
    
        // now build a NSDate object for the input date using these components
        NSDateComponents *components = [[NSDateComponents alloc] init];
        [components setDay:theDay]; 
        [components setMonth:theMonth]; 
        [components setYear:theYear];
        NSDate *thisDate = [gregorian dateFromComponents:components];
        [components release];
    
        // build a NSDate object for aDate next day
        NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
        [offsetComponents setDay:1];
        NSDate *nextDate = [gregorian dateByAddingComponents:offsetComponents toDate:thisDate options:0];
        [offsetComponents release];
    
        [gregorian release];
    
    
        // build the predicate 
        NSPredicate *predicate = [NSPredicate predicateWithFormat: @"startDate < %@ && endDate > %@", nextDate, thisDate];
    
            return predicate;
    
    }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to do THIS , just a little bit more complicated: Lets say,
It's a little more complicated than that, but this is my code: private void
This little bit of syntax has been a bit of a confusion for me
I have this little function function makewindows(){ child1 = window.open (about:blank); child1.document.write(<?php echo htmlspecialchars(json_encode($row2['ARTICLE_DESC']),
I have this little script to toggle a contact form when a button is
I made this little Cocoa app with a WebView that displays Google Maps in
I have this little rake task: namespace :db do namespace :test do task :reset
I have this little problem, that I cannot figure out which arguments to pass
I have this little question that's been on my mind for a while now.
I have this little problem. My client wanted two distinct swf on a web

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.