在 swift 中使用 DateFormatter 时发生错误

While using DateFormatter in swift error occurs

更改日期格式时出现错误。我从服务器获取的日期属于这种类型:2019-08-20T08:05:15.680Z.

let dateFormatter: DateFormatter = DateFormatter()

    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
                    *if let dateFromTimeStamp: Date = dateFormatter.date(from: self.sendMoneyResponseObject.timeStamp!){*
                        dateFormatter.dateFormat = "dd MMM yyyy, hh:mm a"
                        if let dateString: String = dateFormatter.string(from: dateFromTimeStamp) as? String{
                            transactionViewObj.dateLabel.text = dateString
                        }
                    }

需要的日期格式:dd MMM yyyy, HH:mm a

检查你输入的日期格式有误

    let dateFormatter: DateFormatter = DateFormatter()
                              //2019-08-20T08:05:15.680Z.
    dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
     if let dateFromTimeStamp: Date = dateFormatter.date(from: "2019-08-20T08:05:15.680Z"){
        dateFormatter.dateFormat = "dd MMM yyyy, hh:mm a"
        if let dateString = dateFormatter.string(from: dateFromTimeStamp) as? String{
            print ("dateString == \(dateString)")
        }
    }