-3

I am getting error while changing format of date. Date that I'm getting from server is of this type : 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
                        }
                    }

Date format that is needed : dd MMM yyyy, HH:mm a

Karan Khurana
  • 575
  • 8
  • 20
  • 1
    To parse milliseconds use SSS, so your DateFormatter's date format will be yyyy-MM-dd'T'HH:mm:ss.SSSZ – Aditya Mathur Aug 20 '19 at 08:52
  • I don't understand this, why downvote it was specifically explained and I got the perfect answer as well. Just don't downvote for the sake of it. – Karan Khurana Aug 20 '19 at 09:14

1 Answers1

1

check your input Date format is wrong

    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)")
        }
    }
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143