我正在尝试将日期设为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
答案 0 :(得分:2)
使用' SS'说明符(S的数量等于你想得的数字 - 在你的情况下为6)
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ"];
而不是
NSString *newdate = [utcDate stringByReplacingOccurrencesOfString:@"T" withString:@" "];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.Z"];
<强>更新强>
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyyy-MM-dd'T'HH:mm:ss.SSSSSSZ"];
NSDate *msgDate = [dateFormatter dateFromString:@"2017-05-05T04:42:44.954604Z"];
<强>输出强>
答案 1 :(得分:0)
试试这个
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ";// chang
NSDate *date = [dateFormatter dateFromString:@"2017-04-13T05:57:44.886Z"];
NSString * msgDate = [dateFormatter stringFromDate:date];
// if get nil **date** try to change this time format