format a date in to the format 'yyyy-mm-dd hh:mm:ss.fff zzz' in swift

You can format a date string in Swift using DateFormatter. Here is an example of how to format a date in 'yyyy-mm-dd hh:mm:ss.fff zzz' format:

main.swift
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS zzz"
let dateString = dateFormatter.string(from: Date())
print(dateString) // output: "2021-03-06 16:34:29.032 GMT+05:30"
210 chars
5 lines

In the above example, we first create an instance of DateFormatter and set the date format we want using dateFormat property. We then call the string(from:) method on the formatter instance to get the formatted date string. The from parameter is an instance of Date which is the current date and time by calling the Date() function. Finally, we print the formatted date string.

gistlibby LogSnag