Core Data Predicate Date Comparison

前端 未结 5 2129
醉话见心
醉话见心 2020-12-01 04:50

Im trying to fetch all the objects in an entity matching a user selectedDate (it\'s an NSDate). The Core Data code is fine but my predicate keeps returning 0 results, the da

5条回答
  •  眼角桃花
    2020-12-01 05:12

    I can get working with some modifications to the accepted answer and using the iOS 8 API for create dates with time offset. The code:

    NSCalendar *calendar = [[FFSharedCalendar singleton] getGregorianCalendar];
    NSDate *startOfDay = [calendar dateBySettingHour:0 minute:0 second:0 ofDate:[NSDate date] options:0];
    NSDate *endOfDay = [calendar dateBySettingHour:23 minute:59 second:59 ofDate:[NSDate date] options:0];
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"createdAt > %@ AND createdAt < %@", startOfDay, endOfDay];
    NSArray* plans = [Plan MR_findAllWithPredicate:predicate];
    

    Hope it helps someone

提交回复
热议问题