When formatting dates in Swift, occasionally we need to include custom text within the resulting date string. How is that done without creating conflicts with date formatting characters? We can use single quotes (' '
) to escape the text we want to add.
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMMM dd, yyyy 'at' h:mm a"
let currentDate = Date()
print(dateFormatter.string(from: currentDate)) // March 15, 2021 at 8:15 PM