如何将事件添加到默认日历

发布于 2024-12-16 11:43:46 字数 567 浏览 0 评论 0原文

大家好,我需要将事件存储在默认日历中。所以我需要将给定的日期字符串转换为 Nsdate。这里我没有得到正确的格式..有什么帮助吗....我在下面给出了我的代码。 ..

我的日期字符串是 //startdate--format---November 19 2011 9:30 PM 采用这种格式..我已经这样写了

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); 

,我得到了这样的输出,我需要添加它用于活动。我没有得到确切时间...日期是 2011-11-19 07:00:00 +0000

Hi every one i need to keep event to store in default calendar.so i need to convert the given date string into Nsdate.Here i am not getting correct format..could any help....and i am giving my code below...

my date string is //startdate--format---November 19 2011 9:30 PM in this format..i have written like this

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); 

and i am getting ouput like this i need to add it for event.i am not getting exact time...date is 2011-11-19 07:00:00 +0000

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独享拥抱 2024-12-23 11:43:46

试试这个(对我来说效果很好):

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"]];

Try this (that works fine for me):

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);

If you want to set specific time zone then just use method setTimeZone:

[dateformatter setTimeZone:[NSTimeZone timeZoneWithName:@"Europe/Moscow"]];
等风也等你 2024-12-23 11:43:46

最后我找到了错误的解决方案。代码如下

-(IBAction)notifyButtonpressed:(id)sender
{
NSLog(@"通知按钮按下");

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);

}

At last i Found the solution for the error .The Code is below

-(IBAction)notifyButtonpressed:(id)sender
{
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);

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文