我正在尝试按日期过滤 sql lite 数据库结果,但我对 ios 日期和时间对象很糟糕,而且不太确定该怎么做。您将如何设置 NSPredicate 以按今天、过去 7 天、过去 30 天和过去 90 天过滤结果?
谢谢
最佳答案
示例
- (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;
}
关于ios - 如何按 nspredicate 日期过滤 SQL 结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14846376/