ios - 如何按 nspredicate 日期过滤 SQL 结果?

标签 ios date nspredicate

我正在尝试按日期过滤 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/

相关文章:

javascript - 从当前日期添加/减去分钟

swift - 比较 Fetchrequest Xcode Swift OSX Core Data Predicate 中的 NSSet

ios - Swift Model 对象应该调用 API 吗?

ios - 使用 iCloud Key-Value 存储写 App 崩溃

ios - Xcode iOS 照片库应用程序

iphone - 我可以知道ipad是否重新启动吗?

java - 日期年份值在无效时显示有效

java - 从设备获取当前时间毫秒并将其转换为具有不同时区的新日期

ios - 将 NSPredicate 应用于 [(String, Array<String>)]

objective-c - NSPredicate 的结果作为字符串