【发布时间】:2014-08-01 06:49:55
【问题描述】:
我正在使用 EventKit.framework 来获取存储在 EKEventStore 中的事件。
我怎样才能只获取今天的事件,即从早上 00:00AM 到 11:59PM(00:00 到 23:59)。时区可能是任何东西。
我正在使用下面的代码,但它也提供第二天的事件。它比今天的日期增加了 24 小时。
-(void) fetchEvents{
NSDate *startDate = [NSDate date] ;
//Create the end date components
NSDateComponents *tomorrowDateComponents = [[NSDateComponents alloc] init];
tomorrowDateComponents.day = 1;
NSDate *endDate = [[NSCalendar currentCalendar] dateByAddingComponents:tomorrowDateComponents
toDate:startDate
options:0];
// We will only search the default calendar for our events
NSArray *calendarArray = [NSArray arrayWithObject:self.defaultCalendar];
// Create the predicate
NSPredicate *predicate = [self.eventStore predicateForEventsWithStartDate:startDate
endDate:endDate
calendars:calendarArray];
// Fetch all events that match the predicate
NSMutableArray *events = [NSMutableArray arrayWithArray:[self.eventStore eventsMatchingPredicate:predicate]];
}
上述要求如何更改NSPredicate?
【问题讨论】:
-
这应该可以工作,如果你在午夜运行它。 ;-) 每隔一段时间它就会失败,因为
startDate将是“现在”。将创建startDate的代码替换为实际为 creates a NSDate for today midnight 的代码。 -
@MatthiasBauch 我怎样才能得到明天的午夜,即 00AM。如果今天是 7 月 1 日,我怎样才能得到 7 月 2 日的 00AM?
-
您可以使用
NSCalendar方法dateByAddingUnit或dateByAddingComponents从开始日期开始。
标签: objective-c ios7 nspredicate eventkit ekeventstore