我有一个 JSON 格式的日期字符串,它看起来像这样:2013-05-22T10:54:40.437
我正在尝试将其转换为 NSDate。
- (NSString *)dateFromString:(NSString *)datestring{
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss"];
NSDate *date = [dateFormat dateFromString:datestring];
//date is nil here.
NSDateFormatter *newDateFormatter = [[NSDateFormatter alloc]init];
[newDateFormatter setDateFormat:@"MM/dd/yyyy"];
NSString *newString = [newDateFormatter stringFromDate:date];
NSLog(@"Date -- %@",datestring);
return newString;
}
但是 dateFromString 之后的日期为零。有谁知道我在这里做错了什么?谢谢!