-1

For the life of me i cannot seem to convert this nsstring into an nsdate with the dateformatter, its coming from an xml feed with this format:2016-06-03T19:00:00+00:00

I'm using this code but the date is returning null.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US_POSIX"];
    dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss'ZZZZ'";
    NSTimeZone *timeZoney = [NSTimeZone timeZoneWithName:@"UTC"];
    dateFormatter.timeZone = timeZoney;
    NSLog(@"passed: %@",theDate);
    NSDate *convertedDate = [dateFormatter dateFromString:theDate];
    NSLog(@"Date is %@",convertedDate);

I've been using this website http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns to try and figure it out but i can't seem to get it working. Any help would be appreciated.

SmokersCough
  • 967
  • 7
  • 22
  • FYI - If the string provides its own timezone, use it. Don't set a specific timezone on the formatter. – rmaddy Jun 03 '16 at 17:15

1 Answers1

0

You don't want those single quotes around the ZZZZ-- they indicate that the contained letters should appear exactly as you typed them, as with the 'T'. Remove them and the conversion works:

dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZZZZ";
Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
  • It should be `ZZZZZ` for a timezone in format `+00:00`. – rmaddy Jun 03 '16 at 17:14
  • Thanks for the help Tom, apologies for the dupe question, i wasn't aware that the date format had a naming convention... you learn something new everyday! – SmokersCough Jun 04 '16 at 14:48