Why does NSDateFormatter return nil?

痞子三分冷 提交于 2019-12-24 10:48:36

问题


Why is lDateFormatted nil on iPhone simulator 3.1.2 after executing this piece of code?

NSString *lDateString = @"Wed, 17 Feb 2010 16:02:01";
NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy hh:mm:ss"];
NSDate *lDateFormatted = [dateFormatter dateFromString: lDateString ];

Any ideas appreciated, Thanks


回答1:


You probably want to be using HH for hours, that's 0-23 / military style Also, you may need to put single ticks around your comma like ','

Reference: the UTS doc and date formatting guide.




回答2:


The DateFormatter format is correct, but you've probably set the wrong locale. Try this one (working sample code of one of my projects):

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSLocale *enUS = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[formatter setLocale:enUS];
[enUS release];
[formatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss"];
...
[formatter release]



来源:https://stackoverflow.com/questions/2254003/why-does-nsdateformatter-return-nil

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