【发布时间】:2017-05-05 06:10:20
【问题描述】:
我正在尝试获取日期为NSString。我收到格式为 2017-05-05T04:42:44.954604Z
下面是我尝试应用的代码NSDateFormatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSString *newdate = [utcDate stringByReplacingOccurrencesOfString:@"T" withString:@" "];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.Z"];
msgDate = [dateFormatter dateFromString:newdate];
但是msgDate 显示的是nil
【问题讨论】:
-
您的 utcDate 以 ISO 8601 格式提供。看这个问题:stackoverflow.com/questions/17558859/convert-iso-8601-to-nsdate
-
我觉得次数不够,写
2017-05-05T04:42:44.954604Z,下面写yyyy-MM-dd HH:mm:ss.Z。 dateFormat 与字符串日期不匹配有什么问题?那会告诉你你的问题吗? “T”是怎么回事?它在您的 dateFormat 中是如何解释的?等等。
标签: objective-c nsdate nsdateformatter