2013-02-13 85 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