核心数据 - 带日期的谓词

发布于 2024-11-26 10:02:52 字数 412 浏览 2 评论 0原文

我很难尝试为“最近完成”的任务编写谓词,即显示该任务是否在过去 7 天内完成。我想我需要做这样的事情:“if NOW < dateCompleted + 7 days”。

dateCompleted 是表上的一个属性,但我不确定如何获取它的值并从谓词中向其添加 7 天。我想我需要在编写 NSPredicate 之前先获取属性值,但是如何获取呢?我目前无权访问 ManagedObject。

这可能是接近的解决方案,但我不知道如何定义“oneWeek”,并且我不认为您可以在定义谓词时添加值:

NSPredicate *pred = [NSPredicate predicateWithFormat:@"%@ < todoCompletedDate + %@", [NSDate date], oneWeek];

I'm stumped trying to write a predicate for "Recently Completed" tasks, i.e. show the task if it was completed within the last 7 days. I think I need to do something like this: "if NOW < dateCompleted + 7 days".

The dateCompleted is an attribute on the table, but I'm not sure how I'm supposed to get it's value and add 7 days to it from within the predicate. I guess I need to fetch the attribute value first before writing the NSPredicate, but how? I don't have access to the managedObject at this point.

This might be close the solution, but I can't figure out how to define 'oneWeek' and I don't think you can just add values when defining the predicate:

NSPredicate *pred = [NSPredicate predicateWithFormat:@"%@ < todoCompletedDate + %@", [NSDate date], oneWeek];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

硬不硬你别怂 2024-12-03 10:02:52

你快到了。

首先,您需要定义日期范围。为此,您需要从今天的日期开始,然后添加一周的天数以找到有效范围的末尾。一旦确定了该范围,您就可以构建谓词来查找截止日期 >= 开始且 <= 结束的所有任务。这是我编写的一些代码的摘录,用于执行非常类似的操作......

NSDate *today = [NSDate date];
NSDate *startOfToday = [DateHelper startOfDay:today];
NSDate *endOfWeek = [DateHelper addDaysToDate:today daysToAdd:6];
return [NSPredicate predicateWithFormat:@"(dueDate >= %@) AND (dueDate <= %@) AND complete == 0", startOfToday, endOfWeek];

You're almost there.

Firstly you need to define your date range. To do that you'll want to start with today's date and then add a weeks worth of days to find the end of the valid range. Once you have that range you can build your predicate to find all tasks with a due date >= start and <= end. Here's an extract from some code I've written to do something very similar...

NSDate *today = [NSDate date];
NSDate *startOfToday = [DateHelper startOfDay:today];
NSDate *endOfWeek = [DateHelper addDaysToDate:today daysToAdd:6];
return [NSPredicate predicateWithFormat:@"(dueDate >= %@) AND (dueDate <= %@) AND complete == 0", startOfToday, endOfWeek];
末蓝 2024-12-03 10:02:52

获取今天之前一周的数据。 swift代码

let calendar = NSCalendar.currentCalendar()
let now = NSDate()
let nowTimeStamp = self.getCurrentTimeStampWOMiliseconds(now)

let sevenDaysAgo = calendar.dateByAddingUnit(.Day, value: -7, toDate: now, options: [])!
let startDate = calendar.startOfDayForDate(sevenDaysAgo)
let startDateTimeStamp = self.getCurrentTimeStampWOMiliseconds(startDate)

let predicate1 = NSPredicate(format:"(iCreatedAt >= %@) AND (iCreatedAt < %@) AND location = %@", startDateTimeStamp, nowTimeStamp)

就我而言,我的 coreData DB 中有帖子的时间戳值,所以我也使用下面的函数来获取时间戳值

 func getCurrentTimeStampWOMiliseconds(dateToConvert: NSDate) -> String {
        let objDateformat: NSDateFormatter = NSDateFormatter()
        objDateformat.dateFormat = "yyyy-MM-dd HH:mm:ss"
        let strTime: String = objDateformat.stringFromDate(dateToConvert)
        let objUTCDate: NSDate = objDateformat.dateFromString(strTime)!
        let milliseconds: Int64 = Int64(objUTCDate.timeIntervalSince1970)
        let strTimeStamp: String = "\(milliseconds)"
        return strTimeStamp
    }

To get one week before data from today. swift code

let calendar = NSCalendar.currentCalendar()
let now = NSDate()
let nowTimeStamp = self.getCurrentTimeStampWOMiliseconds(now)

let sevenDaysAgo = calendar.dateByAddingUnit(.Day, value: -7, toDate: now, options: [])!
let startDate = calendar.startOfDayForDate(sevenDaysAgo)
let startDateTimeStamp = self.getCurrentTimeStampWOMiliseconds(startDate)

let predicate1 = NSPredicate(format:"(iCreatedAt >= %@) AND (iCreatedAt < %@) AND location = %@", startDateTimeStamp, nowTimeStamp)

In my case i have timestamp value of post in my coreData DB so i use below function also to get timestamp value

 func getCurrentTimeStampWOMiliseconds(dateToConvert: NSDate) -> String {
        let objDateformat: NSDateFormatter = NSDateFormatter()
        objDateformat.dateFormat = "yyyy-MM-dd HH:mm:ss"
        let strTime: String = objDateformat.stringFromDate(dateToConvert)
        let objUTCDate: NSDate = objDateformat.dateFromString(strTime)!
        let milliseconds: Int64 = Int64(objUTCDate.timeIntervalSince1970)
        let strTimeStamp: String = "\(milliseconds)"
        return strTimeStamp
    }
猥琐帝 2024-12-03 10:02:52

要获取一天的开始时间,您可以使用:

NSDate *date = [NSDate date];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *dateComponents = [gregorianCalendar components:(NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear) fromDate:date];
NSDate *startDate = [gregorianCalendar dateFromComponents:dateComponents];

并获取结束日期:

NSCalendar* gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents* dateComponents = [gregorianCalendar components:(NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear) fromDate:date];
[dateComponents setHour:23];
[dateComponents setMinute:59];
[dateComponents setSecond:59];
[dateComponents setDay:[dateComponents day]+7];
NSDate *endDate = [gregorianCalendar dateFromComponents:dateComponents];

To get the start of the day you could use:

NSDate *date = [NSDate date];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *dateComponents = [gregorianCalendar components:(NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear) fromDate:date];
NSDate *startDate = [gregorianCalendar dateFromComponents:dateComponents];

and to get the end date:

NSCalendar* gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents* dateComponents = [gregorianCalendar components:(NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear) fromDate:date];
[dateComponents setHour:23];
[dateComponents setMinute:59];
[dateComponents setSecond:59];
[dateComponents setDay:[dateComponents day]+7];
NSDate *endDate = [gregorianCalendar dateFromComponents:dateComponents];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文