Convert String to Date and Date to string in swift

Convert String to date use following method

func stringToDate(_ str: String, strformate: String) -> Date {
        let formatter = DateFormatter()
        formatter.dateFormat = strformate
        return formatter.date(from: str)!
}

let strDate: String = "20-12-2018"
self.stringToDate(strDate, strformate: "dd-MM-yyyy")

Convert Date to string use following method

func dateToString(_ str: Date, strformate: String) -> String{
        let formatter = DateFormatter()
        formatter.dateFormat = strformate
        return formatter.string(from: str)
}

self.dateToString(dateAppointment, strformate: "dd-MM-yyyy")