format a date in to the format month day, year hour:minute am/pm in swift

You can use DateFormatter to format dates in Swift.

Here's an example of how to format a date to the desired format:

main.swift
let date = Date() // get the current date and time
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMMM dd, yyyy h:mm a" // set the desired date format
let formattedString = dateFormatter.string(from: date)
print(formattedString) // output: "December 01, 2021 1:30 PM"
285 chars
6 lines

In the above code, we first create a variable date that holds the current date and time. Then we create an instance of DateFormatter and set its dateFormat property to the desired format string "MMMM dd, yyyy h:mm a". Finally, we call the string(from: date) method of the dateFormatter instance to get the formatted string. This string is then printed to the console.

gistlibby LogSnag