How to change the string to Time ios swift 3

advertisements

i have five time formate of string. it will come form CSV file.

if my string is

 var myString = "4:20:00 PM"
How can i convert this 4:20 PM

i tried below code

 var time = "5:34:00 PM"
 let dateFormatter = DateFormatter()
 dateFormatter.dateFormat = "HH mm"
 let date = dateFormatter.date(from: time)


Checkout this one

    let timeString = "4:20:00 PM" // String literal of time
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "hh:mm:ss a" //  Date format
    if let stringToDate = dateFormatter.date(from: timeString) {
        print(stringToDate) // prints 2000-01-01 10:50:00 +0000
        dateFormatter.dateFormat = "hh:mm a"
        let dateToString = dateFormatter.string(from: stringToDate)
        print(dateToString) // prints 04:20 PM
    }