2

I am using EventKit.framework to get events stored in EKEventStore.

How can I get only today's events i.e from morning 00:00AM to 11:59PM (00:00 to 23:59). Timezone may be anything.

I am using below piece of code, but it is giving next day event also. It is adding 24 hours to today's date.

-(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]];
}

How to change NSPredicate for above requirement?

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
user2533604
  • 655
  • 1
  • 11
  • 28
  • This should work, if you run it at midnight. ;-) For every other time it'll will fail because `startDate` will be "now". Replace the code that creates `startDate` with something that actually [creates a NSDate for today midnight](http://stackoverflow.com/questions/9040319/how-can-i-get-an-nsdate-object-for-today-at-midnight). – Matthias Bauch Aug 01 '14 at 06:53
  • @MatthiasBauch How can i get tomorrow's midnight i.e 00AM. If today is 1st July, how i can get 2nd July's 00AM? – user2533604 Aug 01 '14 at 07:17
  • You can use `NSCalendar` method `dateByAddingUnit` or `dateByAddingComponents` from the start date. – Rob Aug 14 '14 at 03:09

1 Answers1

4

If you want today's events, there are lots of ways to do it, but you could get the current date/time, and then ask the calendar to set start and end date on the basis of the appropriate hour, minute, and second.

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *now = [NSDate date];
NSDate *startDate = [calendar dateBySettingHour:0  minute:0  second:0  ofDate:now options:0];
NSDate *endDate   = [calendar dateBySettingHour:23 minute:59 second:59 ofDate:now options:0];

NSPredicate *predicate = [store predicateForEventsWithStartDate:startDate
                                                        endDate:endDate
                                                      calendars:nil];

NSArray *events = [store eventsMatchingPredicate:predicate];

If you want endDate to be 12:00am the following day, you can do:

NSDate *endDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:1 toDate:startDate options:0];
Rob
  • 415,655
  • 72
  • 787
  • 1,044