核心日期 - 过滤日期的谓词

时间:2015-07-10 14:25:18

标签: ios core-data nspredicate

我有一个具有日期属性的CD实体。当我执行我的fetchrequest时,我将这个谓词传递给它:

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"(eventStartDate >=%@) AND (eventStartDate <=%@)", startOfDay, endOfDay];

这些是我通过的日期:

First Date:2015-07-10 00:00:00 +0000

Second Date:2015-07-10 11:59:59 +0000

我知道有一个项目符合此查询(在今天午夜之后但在今天的11:59:59之前开始)但谓词不会返回它。如果我用OR条件重写它,它将返回所有以前的日期。如果我只有大于条件,则返回当前和所有未来日期。我怎么写这个,所以我只得到今天符合日期的物品?

由于

以下是我如何格式化secondDate。我确实有11而不是23:

NSDate* today = [[NSDate alloc] init];

NSCalendar* currentCalendar = [NSCalendar currentCalendar];
[currentCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];

NSDateComponents* dateComponents = [currentCalendar components:NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitYear fromDate:today];

NSInteger thisMonth = [dateComponents month];
NSInteger thisDay = [dateComponents day];
NSInteger thisYear = [dateComponents year];

NSDateComponents* endOdDayComponents = [[NSDateComponents alloc] init];
[endOdDayComponents setDay:thisDay];
[endOdDayComponents setMonth:thisMonth];
[endOdDayComponents setYear:thisYear];
[endOdDayComponents setHour:11];
[endOdDayComponents setMinute:59];
[endOdDayComponents setSecond:59];

return [currentCalendar dateFromComponents:endOdDayComponents];

0 个答案:

没有答案