NSPredicate without consider time component

末鹿安然 提交于 2019-12-25 01:49:53

问题


From my table I want select all rows of a particular date without considering the time component of NSDate.Please any one suggest a NSPredicate to achieve this ?


回答1:


NSDate describes an absolute point in time and does not have "day" or "time" components. The same is true for a Core Data "Date" attribute, which is stored as the number of seconds since 1 January 2001, GMT in the database.

What you probably want is to fetch all objects where some "Date" attribute falls into the interval given by the start of a given day and the start of the next day. Therefore you have to compute these dates first, using NSCalendar methods:

NSDate *yourDate = ...;
NSDate *startOfDay, *startOfNextDay;
NSTimeInterval lengthOfDay;
NSCalendar *cal = [NSCalendar currentCalendar];
[cal rangeOfUnit:NSDayCalendarUnit startDate:&startOfDay interval:&lengthOfDay forDate:yourDate];
startOfNextDay = [startOfDay dateByAddingTimeInterval:lengthOfDay];

Then use a predicate that fetches all objects where the attribute falls between these two values:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"date >= %@ AND date < %@",
         startOfDay, startOfNextDay];


来源:https://stackoverflow.com/questions/24186284/nspredicate-without-consider-time-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!