【发布时间】:2013-07-12 15:19:28
【问题描述】:
我需要根据昨天添加的值过滤搜索结果。我昨天看到很多使用:
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:[[NSDate alloc] init]];
[components setHour:-24];
[components setMinute:0];
[components setSecond:0];
NSDate *yesterday = [cal dateByAddingComponents:components toDate:[NSDate date] options:0];
predicate = [NSPredicate predicateWithFormat:@"created_at >= %@", yesterday];
但是,这是从这个确切的时间点算起的 24 小时。我需要过滤昨天的 12:01am-12:00pm。所以昨天的实际 24 小时周期。
我猜我需要按照以下方式做一些事情: 1.取当前日期 2.查找从当前日期到当天12:01am的时间 3. 然后从那个日期减去 24 小时
我有信心能做到#3(当然还有#1),但我不确定如何去做#2。我可能想多了,但我似乎无法掌握如何说:“好吧,现在是上午 8:03,我需要删除 8 小时 2 分钟,这将使我处于上午 12:01”。
【问题讨论】:
标签: objective-c date nsdate nspredicate nscalendar