create a date to iso string in swift

You can create an ISO formatted string from a Date object using the DateFormatter class in Swift. Here is an example:

main.swift
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let date = Date()
let isoString = dateFormatter.string(from: date)
print(isoString)
172 chars
6 lines

In this example, we create a DateFormatter object and set its format to the ISO date format (yyyy-MM-dd'T'HH:mm:ssZ). We then create a Date object using the current date and time, and use the dateFormatter object to convert it to a string in the ISO format. Finally, we print the ISO string to the console.

related categories

gistlibby LogSnag