0

I will add an event to the calendar.

My code is correct but I need the date and the time from Datepicker.

How to set date and time from the datepicker for the following code:

EKEventStore *es = [[EKEventStore alloc] init];
[es requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
    /* This code will run when uses has made his/her choice */
    EKEvent *myEvent = [EKEvent eventWithEventStore:es];
    myEvent.title = @"New Event";

    myEvent.startDate = [NSDate date]; // I need to set date from Datepicker

    NSTimeInterval interval = 60*+5; // I need to set time from Datepicker
    EKAlarm *alarm = [EKAlarm alarmWithRelativeOffset:interval];
    [myEvent addAlarm:alarm];
    myEvent.endDate = [[NSDate date] initWithTimeInterval:600 sinceDate:myEvent.startDate];

    [myEvent setCalendar:[es defaultCalendarForNewEvents]];
    NSError *err;
    [es saveEvent:myEvent span:EKSpanThisEvent error:&err];
}];

You can see the comment where I will set the date and time.

Thanks.

2 Answers2

0

Use (Assuming datePicker as your outlet for UIDatePicker ):

NSDate *date = self.datePicker.date; 
myEvent.startDate = date;

Or in one line :

myEvent.startDate = self.datePicker.date

For time interval:

NSTimeInterval interval = <time in seconds>;

EDIT: (After your comment)

As you need only time, not date+time. Then you need to use NSDateFormatter and format the date.

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
0

I found the solutions for date and time. And I am sharing the code with you:

For date from datepicker:

NSDate *selectedDate = self.datePicker.date;
myEvent.startDate = selectedDate;

For alarm time from datepicker:

EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:selectedDate];
[myEvent addAlarm:alarm];

That's all.

Good Coding ;)