Question

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???

Was it helpful?

Solution

@rmaddy give us the solution:

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top