转换格式的日期字符串:" 2017-01-03 16:50:00.0"到NSDate

时间:2017-01-03 17:40:14

标签: objective-c nsdateformatter

我在使用以下内容时返回nil字符串:

    NSString *formatString = @"yyyy-MM-dd HH:mm:SS.Z";
    NSString *dateString = @"2017-01-03 16:50:00.0";

    NSDateFormatter *newDateFormatter = [[NSDateFormatter alloc] init];
   [newDateFormatter setDateFormat:formatString];
   [newDateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
   [newDateFormatter setLocale:[NSLocale autoupdatingCurrentLocale]];

   NSDate *newDate = [newDateFormatter dateFromString: dateString];

当我记录输出时,我有一个非零日期newDateFormatter,但nil newDate。我做错了什么?

1 个答案:

答案 0 :(得分:1)

当我从格式化程序中删除.Z时,.0从日期工作正常,newDate值为2017-01-03 16:50:00 UTC