NSString (+timezone) 到 NSDate

NSString (+timezone) to NSDate

无法将 NSString 转换为 NSDate

result: date = nil;

(*注意:收到应用内购买收据的日期)

NSString * date_string = @"2017-09-24 11:20:21 Etc/GMT";

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:date_string];
NSLog(@"date %@",date);

试试这个..

    NSString * date_string = @"2017-09-24 11:20:21 Etc/GMT";
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
   [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss VV"];
    NSDate *date = [dateFormatter dateFromString:date_string];
    NSLog(@"date %@",date);

问题是您没有告诉日期格式化程序如何处理日期末尾的有趣字符序列,即 Etc/GMT。你的格式需要在它的末尾有一个 time zone specifierVV 可能会成功,即

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss VV"];