Initializer
init(dateSeparator:dateTimeSeparator:timeZone:)
iOS 15.0+
iPadOS 15.0+
Mac Catalyst 15.0+
macOS 12.0+
tvOS 15.0+
watchOS 8.0+
Creates an instance using the provided date separator, date and time components separator, and time zone.
init(dateSeparator: Date.ISO8601FormatStyle.DateSeparator = .dash, dateTimeSeparator: Date.ISO8601FormatStyle.DateTimeSeparator = .standard, timeZone: TimeZone = TimeZone(secondsFromGMT: 0)!)
Parameters
dateSeparatorThe separator character used between the year, month, and day.
dateTimeSeparatorThe separator character used between the date and time components.
timeZoneThe time zone used to create the string representation of the date.
Discussion
Possible values of dateSeparator are dash and omitted. Possible values of dateTimeSeparator are space and standard.
let aDate = Date()
print(aDate.formatted(Date.ISO8601FormatStyle(dateSeparator: .omitted, dateTimeSeparator: .standard)))
// 20210622T172132Z
if let centralStandardTimeZone = TimeZone(identifier: "CST") {
print(aDate.formatted(Date.ISO8601FormatStyle(dateSeparator: .dash, dateTimeSeparator: .space, timeZone: centralStandardTimeZone)))
}
// 2021-06-22 122132-0500