Ios 按年、月、日筛选的NSPredicate
我使用核心数据来存储数据模型对象。每个对象都有NSDate属性 NSDate属性的格式如下所示:Ios 按年、月、日筛选的NSPredicate,ios,date,core-data,nspredicate,Ios,Date,Core Data,Nspredicate,我使用核心数据来存储数据模型对象。每个对象都有NSDate属性 NSDate属性的格式如下所示: 2013-03-18 12:50:31 +0000 我需要创建一个谓词,该谓词只通过这个值来获取我的对象,而不需要时间。如果您的日期存储为实际日期,那么您应该使用该谓词,而不要随意更改格式。您只需创建一个谓词,检查日期是否在两个日期之间(带时间)。第一个日期是您的日期,时间为00:00:00,第二个日期是之后的一天 // Create your date (without the time) NS
2013-03-18 12:50:31 +0000
我需要创建一个谓词,该谓词只通过这个值来获取我的对象,而不需要时间。如果您的日期存储为实际日期,那么您应该使用该谓词,而不要随意更改格式。您只需创建一个谓词,检查日期是否在两个日期之间(带时间)。第一个日期是您的日期,时间为00:00:00,第二个日期是之后的一天
// Create your date (without the time)
NSDateComponents *yourDate = [NSDateComponents new];
yourDate.calendar = [NSCalendar currentCalendar];
yourDate.year = 2013;
yourDate.month = 3;
yourDate.day = 18;
NSDate *startDate = [yourDate date];
// Add one day to the previous date. Note that 1 day != 24 h
NSDateComponents *oneDay = [NSDateComponents new];
oneDay.day = 1;
// one day after begin date
NSDate *endDate = [[NSCalendar currentCalendar] dateByAddingComponents:oneDay
toDate:startDate
options:0];
// Predicate for all dates between startDate and endDate
NSPredicate *dateThatAreOnThatDay =
[NSPredicate predicateWithFormat:@"(date >= %@) AND (date < %@)",
startDate,
endDate]];
//创建日期(不含时间)
NSDateComponents*yourDate=[NSDateComponents new];
yourDate.calendar=[NSCalendar currentCalendar];
yourDate.year=2013;
yourDate.month=3;
yourDate.day=18;
NSDate*startDate=[yourDate];
//在前一天的基础上增加一天。请注意,1天!=24小时
NSDateComponents*oneDay=[NSDateComponents new];
oneDay.day=1;
//开始日期后一天
NSDate*endDate=[[NSCalendar currentCalendar]dateByAddingComponents:一天
今天:开始
选项:0];
//startDate和endDate之间所有日期的谓词
NSPredicate*当天的日期=
[NSPredicate谓词格式:@“(日期>=%@)和(日期<%@)”,
开始日期,
截止日期]];
虽然David演示了如何创建谓词,但我想添加一种更简单的方法来生成0:00的日期
NSDate *startDate = [NSDate date];
NSTimeInterval lengthDay;
[[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit
startDate:&startDate
interval:&lengthDay
forDate:startDate];
startDate
现在包含一个日期,表示今天当前时区的0:00
NSDate *endDate = [startDate dateByAddingTimeInterval:lengthDay];
现在我们可以把它放到谓词中
NSPredicate *daySpanPredicate = [NSPredicate predicateWithFormat:@"(date >= %@) AND (date < %@)", startDate, endDate];
NSPredicate*daySpanPredicate=[NSPredicate predicate格式:@“(日期>=%@)和(日期<%@)”,开始日期,结束日期];
感谢您的改进。NSString*searchText=@“2013-03-18”;NSPredicate*谓词=[NSPredicate谓词格式:@“%@”,搜索文本];NSMutableArray*predarr=[NSMutableArray arrayWithArray:[totalArr filteredArrayUsingPredicate:predicate]];如果(predarr.count>0){//Write your loic}
NSDate
对象没有格式,如果在某处写入其说明
,格式取决于设备(iOs)配置。+1表示速度比我快,注意1天!=24小时comment@vikingosegundo这是你在这里用同样的逻辑给出的答案,但是oneDay.year=1
?:)@A-Live,是的,我编辑了我在这里发布的代码:我添加了一个与此类似的答案,但使用了另一种创建日期的方法。备注:[yourDate date]
仅在您为NSDateComponents分配了日历的情况下才有效,否则它将返回零(至少在我的测试程序中)。通过使用rangeOfUnit:…
中的interval
参数并将其添加到startofToToToToToDay
中,可以稍微简化startOfToday
的计算。那么您根本不需要任何日期组件。是吗?@martiner,时间间隔不知道DST和闰秒。我的意思是:[[NSCalendar currentCalendar]单位范围:NSDayCalendarUnit startDate:&StartToToToToToDay间隔:&lengthOfToday forDate:startDate]代码>NSDate*startofTomory=[startOfToday-dateByAddingTimeInterval:lengthOfToday]代码>-我在2013年3月31日做了一个快速测试(如你所知,德国DST的开始),并且lengthOfToday
被正确设置为23小时。有趣的是,似乎我误解了间隔参数的用途。但是:它没有捕捉闰秒。至少是我测试的最后4次。