我正在尝试按日期过滤sql lite数据库结果,但我对ios日期和时间对象很糟糕,并且不确定如何做到这一点。您如何设置NSPredicate以过滤今天,过去7天,过去30天和过去90天?
由于
答案 0 :(得分:2)
示例强>
- (NSPredicate *)predicateForDateWithinLast30Days
{
NSDate *currentDate = [NSDate date];// get the current date
// get the date 30 days prior to current date
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:-30];
NSDate *date30DaysAgo = [calendar
dateByAddingComponents:dateComps
toDate:currentDate options:0];
// create the predicate
NSPredicate *last30DaysPredicate = [NSPredicate
predicateWithFormat:@"dateToCompare > %@", date30DaysAgo];
return last30DaysPredicate;
}