0

现在我正在 iPhone 中开发日历应用程序。我正在尝试将事件添加到 iPhone 中的默认日历。在这种情况下,添加事件已成功完成。但我无法在正确的开始时间添加事件。事件的开始日期完全正确,但 iPhone 的默认日历中的时间不同。我使用了以下代码。请帮帮我..

NSLog(@"开始标签为:%@",startLabel.text);// 2013-03-28 12:03:41 PM

NSLog(@"end label is : %@",endLabel.text);//2013-03-28 01:03:41 PM


     NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
     [dateFormat setDateFormat:@"yyyy-MM-dd hh:mm:ss a"];
     [dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
     NSLog(@"description is : %@",[dateFormat description]);


     NSDate *sDate=[dateFormat dateFromString:startLabel.text];      
     NSDate *eDate = [dateFormat dateFromString:endLabel.text];
     NSLog(@"startDate is : %@",sDate);//2013-03-28 12:03:41 +0000
     NSLog(@"endDate is : %@",eDate);// 2013-03-28 13:03:41 +0000
     self.eventStore = [[EKEventStore alloc] init];
     EKEvent *myevent = [EKEvent eventWithEventStore:self.eventStore];
     myevent.title = titleFld.text;
     myevent.location  = locFld.text;
     myevent.startDate = sDate;
     myevent.endDate =  eDate;
     [myevent setCalendar:[eventStore defaultCalendarForNewEvents]];
     NSError *err;
     [self.eventStore saveEvent:myevent span:EKSpanThisEvent error:&err];
4

1 回答 1

3

采用[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];

代替

[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];

希望这能解决您的问题。

于 2013-03-21T06:48:40.237 回答