简体   繁体   English

NSDate与NSDateFormatter返回零日期

[英]NSDate with NSDateFormatter returns nil date

I want to convert a string to an NSDate, like this: 我想将字符串转换为NSDate,如下所示:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateStyle:NSDateFormatterFullStyle];
NSDate *createdAt = [dateFormat dateFromString:[value valueForKey:@"created_at"]];

When I log createdAt it returns nil for some reason and I can't figure out why. 当我登录createdAt时,由于某种原因它返回nil,我不知道为什么。

[value valueForKey:@"created_at"]];
Returns a NSCFString that looks like this:
2013-05-15T05:55:57Z

I have no idea why it's nil, any help would be appreciated! 我不知道为什么它为零,任何帮助将不胜感激!

You should set date format to the NSDateFormatter first. 您应该首先将日期格式设置为NSDateFormatter This should be working: 这应该工作:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *createdAt = [dateFormat dateFromString:[value valueForKey:@"created_at"]];

However I'm not sure about your input date string. 但是我不确定您输入的日期字符串。 The last letter Z should be identification of date's time zone. 最后一个字母Z应该是日期的时区标识。 Letter Z is the format specifier for the time zone according to Unicode Date Format Patterns ( http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns ). 字母Z是根据Unicode Date Format Patternshttp://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns )的时区格式说明符。

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

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