user3660725
user3660725

Reputation: 1

How to set the Predicate for date and time interval in coredata

I am developing one iPad application using storyboard and core data.In my core data i have one table with 5 fields startdate, enddate, starttime, and endtime.The values store in these fields are in gmt values.How i will set predicate for check present time is in between startdate and enddate. and starttime and end time

Sample data is given below.

Table A

name | startdate | enddate | starttime | endtime

A | 31/12/2013 | 31/12/2014 | 5.30 | 17.40

B | 31/12/2011 | 31/12/2012 | 6.30 | 12.40

c | 31/12/2011 | 31/12/2015 | 6.30 | 8.40

here all values is in gmt. i need to check the present date(gmt) is within startdate and enddate.here name 'A' and 'c' is satisfied my condition.if the date is satisfied check current time (gmt) is in-between start time and end time. here 'B' only satisfied this condition.How i write predicate for that

Upvotes: 0

Views: 1048

Answers (2)

VaaChar
VaaChar

Reputation: 688

If you want to fetch only the ones between startdate and enddate use something like this:

NSPredicate *predicate = [NSPredicate
predicateWithFormat:@"startdate >= %@ AND enddate <= %@"
, startdate
, enddate];

If you want to fetch the one between start and enddate, and between starttime and endtime use something like this:

NSPredicate *predicate = [NSPredicate
predicateWithFormat:@"(startdate >= %@ AND enddate <= %@) AND (starttime >= %@ AND endtime <= %@)"
, startdate
, enddate
, starttime
, endtime];

Upvotes: 0

Thanh-Nhon Nguyen
Thanh-Nhon Nguyen

Reputation: 3428

Use TimeInterval, it would be something like this:

NSDate *startDate = ...;
NSDate *endDate =...;

NSDate *currentDate = [NSDate date];
NSTimeInterval currentTimeInterval = [currentDate timeIntervalSince1970];

BOOL isBetween = currentTimeInterval >= [startDate timeIntervalSince1970] && currentTimeInterval <= [endDate timeIntervalSince1970];

Upvotes: -1

Related Questions