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

You can format a date into the desired format using DateFormatter class in Swift. Here's an example:

main.swift
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMMM d, yyyy h:mm:ss.SSS a"
let date = dateFormatter.date(from: "October 13, 2014 11:13:00.000 am")
print(dateFormatter.string(from: date!)) // Output: October 13, 2014 11:13:00.000 AM
249 chars
5 lines

In the above code, we first create an instance of DateFormatter class and set its dateFormat property to the desired format. Then, we convert the input string to a Date object using the date(from:) method of DateFormatter. Finally, we convert the date back to a string using the string(from:) method of DateFormatter with the same format as before. This gives us the desired output "October 13, 2014 11:13:00.000 AM".

gistlibby LogSnag