Question

I'm having trouble formatting a date so I can use it with EventKit to add it to the calendar. The date is coming from JSON from the bandsintown API and is formatted like this:

2014-05-10T19:00:00

My code to add to the calendar is this. The problem is that newDate is null.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
    NSString *formattedDateString = [dateFormatter stringFromDate:date];
    NSDate *newDate = [dateFormatter dateFromString:formattedDateString];

    NSLog(@"%@", newDate);

    EKEventStore *store = [[EKEventStore alloc] init];
    [store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
        if (!granted)
        {
            return;
        }
        EKEvent *event = [EKEvent eventWithEventStore:store];
        event.title = titleString;
        event.location = venueString;
        event.startDate = newDate;
        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];
    }];
Was it helpful?

Solution

You get the date from JSON as a string. Therefore it makes no sense to call stringFromDate: first (and there should be a "incompatible pointer warning"). Only dateFromString: is needed:

NSString *date = @"2014-05-10T19:00:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *newDate = [dateFormatter dateFromString:date];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top