2011-11-16 71 views
0

嗨每一個我需要保持事件存儲在默認calendar.so我需要將給定的日期字符串轉換爲Nsdate.Here我沒有得到正確的格式..可以任何幫助.. ..和我給我下面的代碼...如何將事件添加到默認日曆

我的日期字符串//開始日期 - 格式--- November 19 2011 9:30 PM在此format..i寫了這樣

NSDateFormatter *dateformatter=[[NSDateFormatter alloc]init]; 
[dateformatter setDateFormat:@"MMMM dd yyyy H:m a"]; 

NSLog(@"start date is %@",startdate); 
NSDate *dates=[[NSDate alloc]init]; 
dates=[dateformatter dateFromString:startdate]; 

NSLog(@"date is %@",dates); 

,我越來越像這樣我需要添加它event.i沒有得到確切的時間...日期是2011-11-19 07:00:00 +0000

+0

作爲一個邊點你正在泄漏的分配對象。你不應該爲日期NSDate調用alloc&init來重新分配指針。見Nekto的代碼。 – NJones

回答

2

試試這個(這對我來說工作正常):

NSDateFormatter *dateformatter=[[NSDateFormatter alloc]init]; 
[dateformatter setDateFormat:@"MMMM dd yyyy h:m a"]; 

NSLog(@"start date is %@",startdate); 
NSDate *dates = [dateformatter dateFromString:startdate]; 

NSLog(@"date is %@",dates); 

如果你想設置特定時間段則只需使用方法setTimeZone

[dateformatter setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/Moscow"]]; 
+0

只是爲了澄清。 NSDates在GMT。您的日期格式化程序將解析您在印度當地時間到GMT NSDate的日期。 – NJones

0

最後我找到了錯誤的解決方案。該代碼低於

- (IBAction)notifyButtonpressed:(id)發件人 { NSLog(@「notify button pressed」);

EKEventStore *eventStore = [[EKEventStore alloc] init]; 

EKEvent *event = [EKEvent eventWithEventStore:eventStore]; 
event.title=location; 


//startdate--format---November 19 2011 9:30 PM 

NSDateFormatter *dateformatter=[[NSDateFormatter alloc]init]; 

NSDate *date=[NSDate date]; 
[dateformatter setDateFormat:@"Z"]; 

NSString *timezones=[dateformatter stringFromDate:date]; 

NSLog(@"time zone is %@",timezones); 
NSMutableString *new_start=[[NSMutableString alloc]init]; 



[new_start appendString:[NSString stringWithFormat:@"%@ %@",startdate,timezones]]; 

NSLog(@"after0000-----append is %@",new_start); 


[dateformatter setDateFormat:@"MMMM dd yyyy hh:mm a Z"]; 

NSLog(@"start date is %@",startdate); 
NSDate *startdates = [dateformatter dateFromString:new_start]; 

NSMutableString *new_start1=[[NSMutableString alloc]init]; 


[new_start1 appendString:[NSString stringWithFormat:@"%@ %@",enddate,timezones]]; 


NSDate *enddates=[dateformatter dateFromString:new_start1]; 
NSLog(@"-------date is %@ ,%@------",startdates,new_start1); 


event.title=Title; 



NSLog(@"start date is %@--",startdates); 
event.startDate =[startdates initWithTimeInterval:600 sinceDate:startdates]; 

event.endDate=enddates; 





[event setCalendar:[eventStore defaultCalendarForNewEvents]]; 

NSLog(@"event start date is %@ and enddate is %@",event.startDate,event.endDate); 
NSError *err; 

[eventStore saveEvent:event span:EKSpanThisEvent error:&err]; 

if(!err) 
{ 

    UIAlertView *alertview=[[UIAlertView alloc]initWithTitle:Title message:@"Event Added successfully " delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 

    [alertview show]; 
    [alertview release]; 

} 


NSLog(@"error is %@",err); 

}

相關問題