NSDate -dateFromString返回nil

问题描述:

我从日期格式化程序获取nil。 originTime是2011-08-25 02:12:59,我相信yyyy-MM-dd HH:mm:ss是正确的格式。我的错误是什么?NSDate -dateFromString返回nil

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]; 
assert(locale != nil); 
[dateFormat setLocale:locale]; 
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 
[dateFormat setDateStyle:NSDateFormatterLongStyle]; 
[dateFormat setTimeStyle:NSDateFormatterShortStyle]; 
[dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; 
NSDate *od = [dateFormat dateFromString:originTime]; // nil 
+0

[dateFormat setDateFormat:@“yyyy-MMM-dd HH:mm:ss”];试试这样 –

+0

没有vijay的格式是正确的2行之后是问题 – Robin

只要删除setDateStylesetTimeStyle方法。您自己指定格式(使用setDateFormat)。

+0

谢谢,我不知道他们是相互排斥。 – Morrowless