title: iOS: NSPredicate using NSDate to compare
body: <p>I have an <code>NSDate</code> attribute called <code>startDate</code> stored in the persistence store in the following format (picture below).</p>
  
  <p><img src="https://i.stack.imgur.com/qtDPB.png" alt="enter image description here"></p>
  
  <p>426174354 = July 04, 2014</p>
  
  <p>I need to create (3) <code>NSFetchRequest</code> using predicates.  </p>
  
  <p>For <code>startDate</code>:</p>
  
  <ol>
  <li><code>fetchRequest1</code> using predicate(s) need to fetch everything that falls in today's date based on User's device time.</li>
  <li><code>fetchRequest2</code> using predicate(s) need to fetch everything that's in the past, meaning yesterday and before, based on User's device time.</li>
  <li><code>fetchRequest3</code> using predicate(s) need to fetch everything that's in the future, meaning starting tomorrow and after, based on User's device time.</li>
  </ol>
  
  <p>Below is the code I have so far:</p>
  
  <pre><code>-(NSMutableArray *)getFetchPredicate:(NSUInteger)fetchRequestType
  {
      NSDate *now = [self getcurrentTime:[NSDate date]];
      NSDateFormatter *format = [[NSDateFormatter alloc] init];
      format.dateFormat = @"dd-MM-yyyy";
      format.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
      NSString *stringDate = [format stringFromDate:now];
      NSDate *todaysDate = [format dateFromString:stringDate];
      //today's date is now a date without time. 
  
      NSMutableArray *subpredicates;
  
      if (fetchRequestType == 1)
      {
          NSPredicate *subPredToday = [NSPredicate predicateWithFormat:@"startDate == %@ ", todaysDate];
          [subpredicates addObject:subPredToday];
      }
      else if (fetchRequestType == 2)
      {
          NSPredicate *subPredPast = [NSPredicate predicateWithFormat:@"startDate &lt; %@", todaysDate];
          [subpredicates addObject:subPredPast];
      }
      else if (fetchRequestType == 3)
      {
          NSPredicate *subPredFuture = [NSPredicate predicateWithFormat:@"startDate &gt; %@", todaysDate];
          [subpredicates addObject:subPredFuture];
      }
      return subPredicates;
  }
  
  -(NSDate *)getcurrentTime:(NSDate*)date
  {
      NSDate *sourceDate = date;
  
      NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
      NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
  
      NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
      NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
      NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
  
      NSDate* deviceDateWithTime = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];
      return deviceDateWithTime;
  }
  </code></pre>
  
  <p>The code above is not fetching the right objects from CoreData.  I have a feeling my predicates for comparison are incorrect.  I'm not sure how to convert the stored time in <code>startDate</code> to just Date format only and apply that in Predicates for comparison.  Any suggestions?</p>