Get wrong time when adding an event to default calendar in iPhone

故事扮演 提交于 2019-12-11 16:27:04

问题


Now i am working on calendar application in iPhone. I am trying to add an event to default calendar in iPhone. In this case adding an event is successfully completed.But I can't add an event in proper start time . Start date of event is exactly correct but time is different in default calendar of iPhone.I used the following code .Please help me..

NSLog(@"start label is : %@",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];

回答1:


use [dateFormat setTimeZone:[NSTimeZone systemTimeZone]];

instead of

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

Hope this will solved your problem.



来源:https://stackoverflow.com/questions/15541076/get-wrong-time-when-adding-an-event-to-default-calendar-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!