NSPredicate时间间隔

我有一个表有4个字段startdate,enddate,starttime和endtime。 我需要设置一些谓词

if startdate<= currentdate and currentdate <= enddate if starttime <= currenttime and endtime <= endtime else starttime <= currenttime and currenttime<= 23.00 or 0.00 <= currentime and currentime <= endtime 

我如何设置谓词这种types的条件。当前date和当前时间是我的系统时间gmt。

你可以使用NSCompoundPredicate

创buildNSMutableArray来保存你的谓词及其条件

 NSMutableArray *conditions = [NSMutableArray array]; 

build立你的条件,并将其添加到这个数组

 [conditions addObject:[NSPredicate predicateWithFormat:@"startdate <= %@ AND enddate <= %@",[NSDate date],[NSDate date]]]; 

添加尽可能多的条件,只要你喜欢。 最后build立你的NSCompoundPredicate

 NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:conditions]; 

如果你的startdateenddateNSDate格式,你也可以访问它的时间组件,不需要单独的开始时间和endtime只需比较两个date,就可以了。