To format a date into the desired format, you can use the DateFormatter
class in Swift. Here's an example code block that formats the current date into "month day, year hour:minute:second.fff am/pm" format:
main.swift162 chars5 lines
In the above example code, we created an instance of DateFormatter
and set its dateFormat
property to the desired format string "MMM dd, yyyy h:mm:ss.SSS a". We then used the string(from:)
method of the DateFormatter
instance to format the current date into a string using the set format. Finally, we printed the formatted date string to the console.
gistlibby LogSnag