0

I have column in table with DATE datatype. I want to set predicate in which I want to compare only date without time. Because of time in date I am not getting proper result.

My current code -

[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"((install_date > %@) AND (install_date <= %@))", fromDate,toDate]]
Caleb
  • 124,013
  • 19
  • 183
  • 272
Nikhil
  • 195
  • 2
  • 6
  • asap means as soon as possible bro – jkk Apr 04 '13 at 06:18
  • 4
    @Mak: Thanks, But this should not be an etiquette to ask for any help. And ** after that means? Conditions Apply? That is what I meant. **Please help me.** is good enough. – Anoop Vaidya Apr 04 '13 at 06:21
  • Convert your date in only date format by removing your time & then apply predicate. – Girish Apr 04 '13 at 06:22
  • You just need to change `fromDate` and `toDate` to be the beginning/end of the days, as appropriate. See this question: http://stackoverflow.com/q/13324633/937822 for details on how to do that. – lnafziger Apr 04 '13 at 06:23
  • The NSDate variable always contains the time. The date stored in database also contains the time. I want to compare only date part of DATE stored in database. Currently its comparing yyyy-mm-dd hh:mm:ss = yyyy-mm-dd hh:mm:ss. I dont want to compare time values. – Nikhil Apr 04 '13 at 06:45

2 Answers2

2

I use this function to take out time from NSDate

+(NSDate *)dateWithOutTime:(NSDate *)datDate
{
if( datDate == nil ) {
    datDate = [NSDate date];
}
NSDateComponents* comps = [[NSCalendar currentCalendar] components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:datDate];
return [[NSCalendar currentCalendar] dateFromComponents:comps];

}

My suggestion is take out all values that match and then filter them accordingly by comparing date without time, because you cannot take out date without time in CoreData or you have to store it without time first.

Satheesh
  • 10,998
  • 6
  • 50
  • 93
  • Much easier way of removing time from date `[[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay startDate:&date interval:NULL forDate:date];` PS: This sets the date to start of the day therefore time is set to 00:00:00 – Edwin Nov 09 '15 at 17:41
  • This method will break if the user change the timezone setting. – Dody Rachmat Wicaksono Feb 16 '17 at 17:59
-1

i am using this solution for comparing only date without time in this case my date format is yyyy-MM-dd:

NSDateFormatter * formatter = [NSDateFormatter new];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSString * dateString = [formatter stringFromDate:[NSDate date];
NSDate *rdate = [formatter dateFromString:dateString];

NSPredicate *datePre = [NSPredicate predicateWithFormat:@"date = %@", rdate];
asmad
  • 387
  • 4
  • 13