Get wrong time when adding an event to default cal

2019-08-17 23:51发布

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条回答
女痞
2楼-- · 2019-08-18 00:11

use [dateFormat setTimeZone:[NSTimeZone systemTimeZone]];

instead of

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

Hope this will solved your problem.

查看更多
登录 后发表回答