0

I have this date string:

NSString *dateString = @"2014-05-08T16:48:45.000Z";

and I want convert this one to NSDate.

So, I did this:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *date = [dateFormatter dateFromString:dateString];

=> return: (null)

To understand what create this problem I tried element by element like this:

NSString *dateString = @"2014-05-08T";
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'"]

=> return 2014-05-08 07:00:00 AM +0000 (good!)

But when I had "HH":

 NSString *dateString = @"2014-05-08T16";
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH"]

=> return: (null)

WTF???

Damien Romito
  • 9,801
  • 13
  • 66
  • 84
  • 2
    Set the formatter's locale to the special locale of `en_US_POSIX`. It solves this sort of problem. – rmaddy May 08 '14 at 22:37
  • possible duplicate of [What is the best way to deal with the NSDateFormatter locale "feature"?](http://stackoverflow.com/questions/6613110/what-is-the-best-way-to-deal-with-the-nsdateformatter-locale-feature) – rmaddy May 08 '14 at 22:41

1 Answers1

0

@rmaddy give us the solution:

Set the formatter's locale to the special locale of en_US_POSIX

Damien Romito
  • 9,801
  • 13
  • 66
  • 84