'convert date formate to other date formate include AM and PM

Swift Xcode version 13.2.1

I have date in formate like, "dd/MM/yyyy h:mm:ss ZZZ" in database.I am trying to convert the date acording to the formate in AM and PM using this formate "hh:mm a".But this only show the formate in AM not show PM.

I have code

    let dateEnd = (self.shiftDic[indexPath.row]["endTime"] as? String ?? "")
    let dateFormatter4 = DateFormatter()
    dateFormatter4.dateFormat = "dd/MM/yyyy h:mm:ss ZZZ"
    let endDate = dateFormatter4.date(from: dateEnd)
    dateFormatter4.dateFormat = "hh:mm a"
    let covertEndDate = dateFormatter4.string(from: endDate!)
 //   print("12 hour formatted Date:",covertEndDate)`enter code here`

I want both AM and PM formate.Please any one help!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source