objective c - Core Data Predicate Date Comparison -
im trying fetch objects in entity matching user selecteddate (it's nsdate). core data code fine predicate keeps returning 0 results, dates same in database user selecting.
how should selecteddate compared date entity using predicate?
nspredicate *predicate = [nspredicate predicatewithformat:@"(edate = %@)", selecteddate];
your predicate looks fine.
the reason you're finding 0 result returned may dates aren't entirely same.
for example:
05/04/2012 13:37:00
not match 05/04/2012 13:37:01
these 2 values not same.
do want check date (day, month, year) time?
if want check date, should create start date , end date , compare them using user selected date frame of reference.
something similar should create date , time 00:00:00.
//gather current calendar nscalendar *calendar = [nscalendar currentcalendar]; //gather date components date nsdatecomponents *datecomponents = [calendar components:(nsyearcalendarunit | nsmonthcalendarunit | nsdaycalendarunit) fromdate:[nsdate date]]; //set date components [datecomponents sethour:0]; [datecomponents setminute:0]; [datecomponents setsecond:0]; //return date relative date return [calendar datefromcomponents:datecomponents];
create date setting hours, minutes , seconds 23:59:59 , check user selected date falls between these ranges.
[nspredicate predicatewithformat:@"date between %@", [nsarray arraywithobjects:startofday, endofday, nil]]
Comments
Post a Comment