输入NSDate的格式

时间:2013-10-30 22:57:42

标签: ios nsdate icalendar

这就是我向Ical添加一个事件的方法我只是不知道如何使开始日期不是当天,而是将持续时间设置为全天事件。

> EKEventStore *store = [[EKEventStore alloc] init];
>         [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
>             if (!granted) { return; }
>             EKEvent *event = [EKEvent eventWithEventStore:store];
>             event.title = @"Hits THermal DC I";
>             event.startDate = [NSDate date]; //today
>             event.endDate = [event.startDate dateByAddingTimeInterval:60*60];  //set 1 hour meeting
>             [event setCalendar:[store defaultCalendarForNewEvents]];
>             NSError *err = nil;
>             [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
>             NSString *savedEventId = event.eventIdentifier;  //this is so you can access this event later
>         }];

1 个答案:

答案 0 :(得分:1)

要使用您自己的值创建任何日期,请执行以下操作:

NSCalendar* calendar=[NSCalendar currentCalendar];    

NSDateComponents* dateComps=[[NSDateComponents alloc] init];

[dateComps setYear:1990];
[dateComps setMonth:11];
[dateComps setDay:10];

NSDate* starDate=[calendar dateFromComponents:dateComps];

EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
    if (!granted) { return; }
    EKEvent *event = [EKEvent eventWithEventStore:store];
    event.title = @"Hits THermal DC I";
    event.startDate = starDate
    event.endDate = [event.startDate dateByAddingTimeInterval:60*60];  //set 1 hour meeting
    [event setCalendar:[store defaultCalendarForNewEvents]];
    NSError *err = nil;
    [store saveEvent:event span:EKSpanThisEvent commit:YES error:&err];
    NSString *savedEventId = event.eventIdentifier;  //this is so you can access this event later
}];