8

I'm trying to get the NSDate from a string with the following format 'YYYY-MM-ddTHH:mm:ssZ' using the NSDateFormatter. The NSDateFormatter returns always nil. Here is how I tried to do that:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
NSDate *dateFromString = [dateFormatter dateFromString:@"2013-08-09T18:30:00+02:00"];
Tulon
  • 4,011
  • 6
  • 36
  • 56
andreaspfr
  • 2,298
  • 5
  • 42
  • 51
  • 2
    Hint: The date string does not end with a literal 'Z'. - And 'YYYY' should probably be 'yyyy'. – Martin R May 27 '14 at 20:49
  • I'm not sure whether you need to escape characters like : - in '' marks – Julian May 27 '14 at 20:53
  • 1
    Hint: Consult the [date format string spec](http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns). – Hot Licks May 27 '14 at 21:12

2 Answers2

22

that would be a better formatter.

[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
holex
  • 23,961
  • 7
  • 62
  • 76
0

Your date format string should be as follows:

@"yyyy'-'MM'-'dd'T'HH':'mm':'ssZ"

Note the lower cased 'yyyy'. The uppercase Y means "Week of Year" based calendar.

See this answer: Difference between 'YYYY' and 'yyyy' in NSDateFormatter and the Unicode standard for more info: http://www.unicode.org/reports/tr35/tr35-19.html#Date_Format_Patterns

Community
  • 1
  • 1
danielbeard
  • 9,120
  • 3
  • 44
  • 58