NSDateFormatter problem
I am trying to get NSDate from string but its returning nil. I tried this开发者_运维技巧:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-mm-dd'T'HH:mm:ss'Z'"];
NSDate *date = [dateFormatter dateFromString:@"2010-05-07T10:12:32UTC"];
NSLog(@"Success date=%@",[date description]);
Thanks
Your date format string expects the date to end with a literal Z
. But your date ends with the string UTC
. There are several ways to fix this.
- Change your date format string to
@"yyyy-mm-dd'T'HH:mm:ss'UTC'"
. - Or, change your date string to
@"2010-05-07T10:12:32Z"
.
Or, you could change your date format string to: @"yyyy-mm-dd'T'HH:mm:ssz"
. This will recognize some common 3-letter time zone names, such as PDT for Pacific Daylight Time. Unfortunately, however, it will not recognize UTC as a time zone name (you’d have to use “GMT”).
The literal 'Z'
won't match the string UTC
.
精彩评论