I am using below piece of code to fetch calendar events and to log it's details.When i try to log currenttime, startDate and endDate,it is displaying time around 5 hours before. I am not getting what is the reason behind it?
I have set an event at 30th July from 8PM to 9PM.
My Code:
- (NSMutableArray *)fetchEvents
{
long hour;
long minute;
NSCalendar *cal=[NSCalendar autoupdatingCurrentCalendar];
NSTimeZone *tz=[NSTimeZone timeZoneWithName:@"Asia/Kolkata"];
[cal setTimeZone:tz];
NSDate *currentTime = [NSDate date];
NSLog(@"currentTime %@", currentTime);
NSDateComponents *comp=[cal components:NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:now];
hour = [comp hour];
minute =[comp minute];
NSLog(@"hour %ld", hour);
NSLog(@"minute %ld", minute);
//Create the end date components
NSDateComponents *tomorrowDateComponents = [[NSDateComponents alloc] init];
tomorrowDateComponents.day = 2;
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]];
NSLog(@"events %@", events);
[events valueForKey:@"title"];
[events valueForKey:@"location"];
[events valueForKey:@"timeZone"];
[events valueForKey:@"startDate"];
[events valueForKey:@"endDate"];
NSLog(@"title %@", [events valueForKey:@"title"]);
NSLog(@"location %@", [events valueForKey:@"location"]);
NSLog(@"timeZone %@", [events valueForKey:@"timeZone"]);
NSLog(@"startDate %@", [events valueForKey:@"startDate"]);
NSLog(@"endDate %@", [events valueForKey:@"endDate"]);
return events;
}
But when i run above code, i am getting following output.
2014-07-29 14:13:57.979 iCalEvents[806:60b] currentTime 2014-07-29 08:43:57 +0000 2014-07-29 14:13:57.980 iCalEvents[806:60b] hour 14 2014-07-29 14:13:57.980 iCalEvents[806:60b] minute 13 2014-07-29 14:13:57.981 iCalEvents[806:60b] startDate 2014-07-29 08:43:57 +0000 2014-07-29 14:13:57.990 iCalEvents[806:60b] events ( "EKEvent <0x95485c0>\n{\n\t EKEvent <0x95485c0>\n{\t title = \t\tMyEvent; \n\t location = \t(null); \n\t calendar = \tEKCalendar <0x9458b90> {title = Calendar; type = Local; allowsModify = YES; color = #1BADF8;}; \n\t alarms = \t\t(null); \n\t URL = \t\t\t(null); \n\t lastModified = 2014-07-29 08:38:32 +0000; \n\t timeZone = \tAsia/Kolkata (GMT+5:30) offset 19800 \n}; \n\t location = \t(null); \n\t startDate = \t2014-07-30 14:30:00 +0000; \n\t endDate = \t\t2014-07-30 15:30:00 +0000; \n\t allDay = \t\t0; \n\t floating = \t0; \n\t recurrence = \t(null); \n\t attendees = \t(null) \n};" ) 2014-07-29 14:13:57.990 iCalEvents[806:60b] title ( MyEvent ) 2014-07-29 14:13:57.991 iCalEvents[806:60b] location ( "" ) 2014-07-29 14:13:57.991 iCalEvents[806:60b] timeZone ( "Asia/Kolkata (GMT+5:30) offset 19800" ) 2014-07-29 14:13:57.992 iCalEvents[806:60b] startDate ( "2014-07-30 14:30:00 +0000" ) 2014-07-29 14:13:57.992 iCalEvents[806:60b] endDate ( "2014-07-30 15:30:00 +0000" )