How can I convert the 24 hour time format to 12 hour time format?

2 Solutions Collect From Internet About “How can I convert the 24 hour time format to 12 hour time format?”

As you say, you should use NSDateFormatter for your desired format. Hope this will help you.

let date = NSDate()
let formatter = NSDateFormatter()
formatter.dateFormat = "hh:mm a"
let time = formatter.stringFromDate(date)

For more information about date formatter see NSDateFormatter

Swift 3.1 Answer

 let date = Date()                //Jun 1, 2017, 10:22 AM"
 let formatter = DateFormatter()  //<NSDateFormatter: 0x608000244380>
 formatter.dateFormat = "hh:mm a" //<NSDateFormatter: 0x608000244380>
 let time = formatter.string(from: date) //"10:22 AM"