我正在使用下面的代码来获取日历事件并记录它的详细信息。当我尝试记录currenttime,startDate和endDate时,它显示的时间大约是5个小时。我不知道它背后的原因是什么?
我已于7月30日晚上8点到晚上9点举办活动。
我的代码:
- (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;
}
但是当我运行上面的代码时,我会得到以下输出。
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]小时14 2014-07-29 14:13:57.980 iCalEvents [806:60b]分钟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]事件( “EKEvent< 0x95485c0> \ n {\ n \ t EKEvent< 0x95485c0> \ n {\ t title = \ t \ tTyEvent; \ n \ t location = \ t(null); \ n \ t calendar = \ tEKCalendar < 0x9458b90> {title = Calendar; type = Local; allowsModify = YES; color =#1BADF8;}; \ n \ t alarms = \ t \ t \ t \ t \ t \ t \ t; \ n \ t \ tdate = \ t \ t \ t \ tt (null); \ n \ t lastModified = 2014-07-29 08:38:32 +0000; \ n \ t timeZone = \ tAsia / Kolkata(GMT + 5:30)偏移量19800 \ n}; \ n \ t location = \ t(null); \ n \ t startDate = \ t 2014-07-30 14:30:00 +0000 ; \ n \ t endDate = \ t \ t 2014-07-30 15:30:00 +0000 ; \ n \ t \ tallDay = \ t \ tt \ t \ t \ t \ t \ n \ t \ ttomp = = t0; \ n \ t recurrence = \ t(null); \ n \ t与会者= \ 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]位置( “” ) 2014-07-29 14:13:57.991 iCalEvents [806:60b] timeZone( “亚洲/加尔各答(格林尼治标准时间+5:30)抵消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” )