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!
You should set date format to the NSDateFormatter
first. 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. 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 ).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.