-1

I can't parse date from string via NSDateFormatter. The date string is @"2014-01-21T20:00:36+04:00". I am trying to use this format string: @"yyyy-MM-ddTHH:mm:ss" but it doesn't work. Please help me.

The full code is:

NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-ddTHH:mm:ssZ";
NSDate * date = [dateFormatter dateFromString:dateString];
Alexander Perechnev
  • 2,797
  • 3
  • 21
  • 35
  • See [here](http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns) for date format patterns. And also see [this question](http://stackoverflow.com/questions/6613110/what-is-the-best-way-to-deal-with-the-nsdateformatter-locale-feature) for info on something that will bite you if you don't watch out. – Hot Licks Jan 22 '14 at 23:28
  • 1
    And most people put the T in single quotes, though I'm not sure it's necessary. – Hot Licks Jan 22 '14 at 23:29
  • 1
    BTW, this is probably about #3 on the most asked questions list, so be nice or we'll vote you down and close the question as a dupe. – Hot Licks Jan 22 '14 at 23:31
  • @HotLicks sorry i was really confused by this problem and didn't know how to solve :) – Alexander Perechnev Jan 22 '14 at 23:34

2 Answers2

1

add Z at the end @"yyyy-MM-dd'T'HH:mm:ssZZZZZ" as you have timezone included

Julian
  • 9,299
  • 5
  • 48
  • 65
0

Following on from http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns

NSString *dateString = @"2014-01-21T20:00:36+04:00";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];

dateFormat.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZZZZZ";
NSDate *date = [dateFormat dateFromString:dateString];

NSLog(@"Parsed date is %@", date);

yields the following output which seems correct

Parsed date is 2014-01-21 16:00:36 +0000
Niels Castle
  • 8,039
  • 35
  • 56