format a date in to the format october 13, 2014 11:13 am in swift

You can format a date in Swift using DateFormatter class. Here's how to format a date in the desired format:

main.swift
let dateFormatterGet = DateFormatter()
dateFormatterGet.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateFormatterPrint = DateFormatter()
dateFormatterPrint.dateFormat = "MMM dd,yyyy hh:mm a"

if let date = dateFormatterGet.date(from: "2019-08-12 22:31:58") {
    print(dateFormatterPrint.string(from: date))
} else {
   print("There was an error decoding the string")
}
365 chars
11 lines

In the above example, dateFormatterGet is used to convert a string formatted date into a Date object. Then, dateFormatterPrint is used to format the date into the desired format.

The output of the code above would be: Aug 12,2019 10:31 PM.

gistlibby LogSnag