简体   繁体   English

NSDate -dateFromString返回nil

[英]NSDate -dateFromString returns nil

I am getting nil from the date formatter. 我从日期格式化程序得到零。 originTime is 2011-08-25 02:12:59, and I believe yyyy-MM-dd HH:mm:ss is the correct format for that. originTime是2011-08-25 02:12:59,我相信yyyy-MM-dd HH:mm:ss是正确的格式。 What is my mistake? 我的错是什么?

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

Just delete the setDateStyle and setTimeStyle methods. 只需删除setDateStylesetTimeStyle方法即可。 You are specifying the format on your own (with setDateFormat ). 您可以自己指定格式(使用setDateFormat )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM