设置event.startDate

时间:2012-12-02 18:24:36

标签: objective-c cocoa nsdata

这是我的代码!但event.startDate返回2012-12-29 18:05:00 +0000

数据是一个字符串=“04-12-2012”

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
 [dateFormat setDateFormat:@"dd-MM-YYYY HH:mm"];
 NSDate *date = [dateFormat dateFromString:[NSString stringWithFormat:@"%@ %@",data,oraInizio]];
 NSDate *date2 = [dateFormat dateFromString:[NSString stringWithFormat:@"%@ %@",data,oraFine]];

event.startDate=date;
event.endDate=date2;

为什么2012-12-29而不是2012-12-04 ??

这是完整的脚本

- (IBAction)aggiungiEvento:(id)sender {

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

    if([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
     // iOS 6 and later
     [eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        if (granted){

            EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
            event.title     = [NSString stringWithFormat:@"%@ al %@",tipoLocale,nomeLocale];

            NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
            [dateFormat setDateFormat:@"dd-MM-YYYY HH:mm"];

            NSLog(@"%@   %@",data,oraInizio);

            NSDate *date = [dateFormat dateFromString:[NSString stringWithFormat:@"%@ %@",data,oraInizio]];
            NSDate *date2 = [dateFormat dateFromString:[NSString stringWithFormat:@"%@ %@",data,oraFine]];


            event.startDate=date;
            event.endDate=date2;

            [event setCalendar:[eventStore defaultCalendarForNewEvents]];
            NSError *err;
            [eventStore saveEvent:event span:EKSpanThisEvent error:&err];

             NSLog(@"%@  %@",event.startDate,data);

        }else
        {
            //----- codes here when user NOT allow your app to access the calendar.
        }
    }];
}

1 个答案:

答案 0 :(得分:0)

这是因为您在格式的年份部分应该使用@“@"YYYY"”时使用了yyyy

请注意,即使你修复了这个问题,如果你没有做关于指定时区的事情,你的时间可能仍然是错误的。