Reputation: 16841
I need to write a NSPredicate to see if the provided date falls between a certain time internal.
For example if i provide the data 12 Mar, 2012
. It needs to see if it falls between the startdate
and enddate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(statrDate >= %@) AND (enddate <= %@)", providedDate, ProvidedDate];
1.) in the above example startdate
is 10 Mar 2012
, and endDate
is 20 Mar 2012
. But the NSPredicate
doesn't return anything because i have specified 12 Mar 2012 as providedDate
.
2.) Can i make use of the BETWEEN
clause so this scenario
Upvotes: 1
Views: 835
Reputation: 920
Use a compound predicate:
NSPredicate *greaterThanPredicate = [NSPredicate predicateWithFormat:@"statrDate <= %@", providedDate];
NSPredicate *lessThanPredicate = [NSPredicate predicateWithFormat:@"enddate >= %@", providedDate];
NSPredicate *betweenPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:greaterThanPredicate, lessThanPredicate, nil]];
I used >=
and <=
because you did. If it's not supposed to be inclusive, switch to >
and <
respectively.
Upvotes: 1
Reputation: 3907
Just remove brackets and try :
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"statrDate >= %@ AND enddate <= %@", providedDate, ProvidedDate];
Upvotes: 1