4

I'm trying to find the answer everywhere including StackOverflow.. I have a problem adding a time event from a string which looks like this: 2014-12-31T19:00:00-06:00. I'm parsing this from XML by the way. Rest of the code, including labels, etc. work fine.

So here is my code for adding a new events to the calendar (I tried to add 'dateFromString' already as a name with a current time to check and it goes through):

EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
    if (!granted) { return; }
    EKEvent *event = [EKEvent eventWithEventStore:store];
    event.title = tempNameOfEvent;
    event.startDate = dateFromString; //here is a NSDate but incorrectly formatted
    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];
}];

Code from Programmatically add custom event in the iPhone Calendar

So, how should I format the NSDate to work with EKEventStore? Should I try to add it as "yyyy-MMM-dd HH:mm:ss ZZZ" which looks like "2007-01-09 17:00:00 +0000"? How to parse string and eliminate part with "-06:00"? (given time from XML looks like this: 2014-12-31T19:00:00-06:00).

Community
  • 1
  • 1
Maciej
  • 111
  • 5

2 Answers2

1

Maybe you can use Google?? https://developers.google.com/google-apps/calendar/v3/reference/events/insert This will be a good idea because you will be able to do this: calendarId string Calendar identifier.

alexkrause88
  • 144
  • 4
0

You have the wrong format for the month, should be 'MM', missing quoting of the 'T' and adding a unneeded space prior to 'xxx'.

Use: yyyy-MM-dd'T'HH:mm:ssxxx

See: ICU Formatting Dates and Times
Also: Date Field SymbolTable.

Example:

NSString *ds = @"2014-12-31T19:00:00-06:00";
NSDateFormatter *df = [NSDateFormatter new];
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssxxx"];
NSDate *d = [df dateFromString:ds];
NSLog(@"d: %@", d);

Output:

d: 2015-01-01 01:00:00 +0000

The output form is just the default display provided by NSDate, if you want a particular format use a NSDateFormatter to create the string to be displayed.

Note that the date displayed is in UTC so it is in the next year for the supplied date. ;-)

You probably do not want to eliminate the timezone, NSDates are the time since the first instant of 1 January 2001, GMT so the timezone is really necessary unless you are doing something rather non-standard.

zaph
  • 111,848
  • 21
  • 189
  • 228