-->

NSDateFormatter dateFromString:stringDate returnin

2020-04-16 01:21发布

问题:

I have this code

NSString *stringDate = @"2015-07-09 7:00 AM";
NSString *stringDateFormat = @"yyyy-MM-dd h:mm a";
NSTimeZone *timeZone =  [NSTimeZone localTimeZone];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:stringDateFormat];
NSDate *dateFormatted = [dateFormatter dateFromString:stringDate];

The problem now is dateFormatted keeps getting nil value. It is OK when I try it in Simulator iPhone5s 8.2 but, the problem occurs when I run it with device iPhone5s 8.3.

Can someone help me find why is dateFormatted is getting nil value?

回答1:

It's almost a canned response but: QA1480. Set your formatter's locale to en_US_POSIX or its behaviour will be dependent upon the user's settings.