2013-02-13 59 views

回答

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; 
} 
+0

谢谢,非常完美! – user2067051 2013-02-16 14:18:51

+0

我需要类似的东西,除了在接下来的30天内。修改了一下,非常感谢! – RyanG 2013-04-22 15:44:34

+0

嗨朋友..我是新的谓词概念..我需要实现这个概念..我需要我的费用被过滤基于日期..什么时候这个NSpredicate方法将被调用..如何实现这.. .. – Mano 2013-07-19 05:27:00