如何使用NSDate在一个月内构build一个NSPredicate整天过滤?

有没有办法让一个月的所有日子一般?

像这样的东西:

[NSPredicate predicatewithFormat@"(date.firstDayOfMonth >= @)AND(date.lastDayOfMonth <= @)"];

是。 你需要弄清楚本月的第一个瞬间 ,以及下个月的第一个瞬间。 以下是我将如何做到这一点:

 NSDate *now = [NSDate date]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *nowComponents = [calendar components:NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:now]; [nowComponents setDay:1]; NSDate *beginningOfCurrentMonth = [calendar dateFromComponents:nowComponents]; NSDateComponents *oneMonth = [[NSDateComponents alloc] init]; [oneMonth setMonth:1]; NSDate *beginningOfNextMonth = [calendar dateByAddingComponents:oneMonth toDate:beginningOfCurrentMonth options:0]; 

一旦你有了这两个NSDate ,那么你可以这样做:

 [NSPredicate predicateWithFormat:@"date >= %@ AND date < %@", beginningOfCurrentMonth, beginningOfNextMonth];