title: Filtering a NSArray containing objects with a NSDate field with NSPredicate
body: <p>I have an array of objects, with each object containing a field called date.  I want to create a subset of this array using <code>NSPredicate</code> where I pull only those objects that fall within a certain date range.  Here's the code I am using:</p>
  
  <pre><code>predicate = [NSPredicate predicateWithFormat:@"(date &gt;= %@) AND (date =&lt; %@)", startDate, endDate];
  _daysArray = [_cachedDaysArray filteredArrayUsingPredicate:predicate];
  </code></pre>
  
  <p>The problem I am having is when <code>startDate</code> and <code>endDate</code> are the same day, but different times.  Here's the variables view in the XCode debugger</p>
  
  <p><img src="https://i.stack.imgur.com/qnwjW.png" alt="enter image description here"></p>
  
  <p>As you can see, I have an object in the array that has a <code>date</code> field equal to <code>startDate</code>, but you can also see that <code>_daysArray</code> contains no elements after the filter is executed.</p>
  
  <p>This works fine when the dates are not the same day.</p>
  
  <p>Any help would be greatly appreciated.</p>
  
  <p><strong>EDIT #1</strong></p>
  
  <p>Here's the <code>po</code> of the <code>predicate</code> variable as requested by commenter:</p>
  
  <pre><code>(lldb) po predicate
  date &gt;= CAST(406616400.676014, "NSDate") AND date &lt;= CAST(406702799.677098, "NSDate")
  (lldb)
  </code></pre>
  
  <p><strong>EDIT #2</strong></p>
  
  <pre><code>(lldb) po [[_cachedDaysArray firstObject] date]
  2013-11-18 05:00:00 +0000
  (lldb) 
  </code></pre>
  
  <p><strong>EDIT #3</strong></p>
  
  <p>Martin R. got me on the right track with his comment on how <code>startDate</code> was being calculated.  Now that I have changed that, it's working.  I am using a method to calculate <code>startDate</code>.  Previously, I was using <code>NSUIntegerMax</code> to indicate the components, now I am breaking them out specifically:</p>
  
  <pre><code>-(NSDate *)pastDate:(NSInteger)daysPast
  {
      // Get the appropriate calendar
      NSCalendar *calendar = [NSCalendar currentCalendar];
  
      NSDateComponents *startComponents = [calendar components:(NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitYear|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond) fromDate:[NSDate date]];
      [startComponents setHour: 00];
      [startComponents setMinute: 00];
      [startComponents setSecond: 00];
      startComponents.day = startComponents.day - daysPast;
  
      return [calendar dateFromComponents:startComponents];
  }
  </code></pre>