开发者

NSDate -dateFromString returns nil

开发者 https://www.devze.com 2023-03-31 11:48 出处:网络
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. What is my mistake?

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. 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 ti开发者_JAVA百科meZoneForSecondsFromGMT:0]];
NSDate *od = [dateFormat dateFromString:originTime]; // nil


Just delete the setDateStyle and setTimeStyle methods. You are specifying the format on your own (with setDateFormat).

0

精彩评论

暂无评论...
验证码 换一张
取 消