1

我正在使用下面的代码来获取日历事件并记录它的详细信息。当我尝试记录当前时间、开始日期和结束日期时,它会显示大约 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 标题 = \t\tMyEvent; \n\t 位置 = \t(null ); \n\t 日历 = \tEKCalendar <0x9458b90> {title = Calendar; type = Local; allowedModify = YES; color = #1BADF8;}; \n\t 警报 = \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) 偏移19800 \n}; \n\t 位置 = \t(null); \n\t开始日期= \t2014-07-30 14:30:00 +0000 ; \n\t结束日期= \t\t 2014-07-30 15:30:00 +0000 ; \n\t allDay = \t\t0; \n\t 浮动 = \t0; \n\t 重复 = \t(null); \n\t 参加者 = \t(null) \n};" ) 2014-07-29 14:13:57.990 iCalEvents[806:60b] 标题 (MyEvent) 2014-07-29 14:13:57.991 iCalEvents[806 :60b] 位置 ("") 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" )

4

0 回答 0