2

I seem to be running into great difficulty in attempting to convert a Google Calendar date string (parsed from a public JSON), into NSDate format.

The result from Google's Calendar API is in the format:

"2012-06-20T11:00:00.000+01:00"

I'm trying to format this as an NSDate, as follows:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSSzzz";
NSDate *gmtDate = [formatter dateFromString:[whenDict objectForKey:@"startTime"]];

However, I keep getting a null result, so I was wondering if anyone knows what the correct [NSDate dateformat] would be for the result that I'm trying to convert...

I've tried all sorts of dateFormat variations but I haven't managed to find the right one, after a couple of hours of messing around with multiple letter variations!

i.e. I believe the issue is in the line:

formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSSzzz";

Cheers,

Sarreph
  • 1,986
  • 2
  • 19
  • 41

1 Answers1

6

It seems that +01:00 is not a standard time zone so you will have to change it to +0100 removing the colon from it (refer to iPhone NSDateFormatter Timezone Conversion)

You could use the following code

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSSZZZ";
NSString *dataString = @"2012-06-20T11:00:00.000+01:00";
NSMutableString *mutableDate = [dataString mutableCopy];
[mutableDate deleteCharactersInRange:NSMakeRange(mutableDate.length - 3, 1)];

NSDate *gmtDate = [formatter dateFromString:mutableDate];
Community
  • 1
  • 1
Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56