NSDateFormatter dateFromString:stringDate returning nil in iOS 8.3

核能气质少年 提交于 2019-12-02 05:57:33

问题


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.



来源:https://stackoverflow.com/questions/31317076/nsdateformatter-datefromstringstringdate-returning-nil-in-ios-8-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!