NSPredicate时间间隔 [英] NSPredicate for time interval
问题描述
我有一个表有4个字段startdate,enddate,starttime和endtime。我需要设置谓词
I have one table with 4 fields startdate ,enddate, starttime and endtime. I need to set predicate something like
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
如何为这种类型的条件设置谓词。当前日期和当前时间是我的系统时间(以gmt为单位)。
How i set predicate for this type of condition.Current date and current time is the my system time in gmt.
推荐答案
您可以使用 NSCompoundPredicate
创建 NSMutableArray
使用条件保存您的谓词
Create NSMutableArray
to hold your predicates with their conditions
NSMutableArray *conditions = [NSMutableArray array];
创建条件并将它们添加到此数组
Build your conditions and add them to this array
[conditions addObject:[NSPredicate predicateWithFormat:@"startdate <= %@ AND enddate <= %@",[NSDate date],[NSDate date]]];
添加任意多个条件。最后建立您的 NSCompoundPredicate
Add as many conditions as you like. Finally build your NSCompoundPredicate
NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:conditions];
如果您有 startdate
c $ c> enddate in NSDate
格式,你也可以访问它的时间组件,没有必要单独的 starttime
和 endtime
只是比较两个日期,应该可以。
If you have your startdate
and enddate
in NSDate
format you can access it time component as well, there is no need of separate starttime
and endtime
just compare the two dates and should be ok.
这篇关于NSPredicate时间间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!