0

I want to convert a string to an NSDate, like this:

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.

[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!

4

1 回答 1

1

您应该将日期格式设置为NSDateFormatter第一个。这应该有效:

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

但是我不确定您的输入日期字符串。最后一个字母Z应该是日期时区的标识。LetterZ是根据Unicode Date Format Patterns( http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns ) 的时区格式说明符。

于 2013-05-27T07:51:43.113 回答