Date.ISO8601FormatStyle
A type that converts between dates and their ISO-8601 string representations.
Declaration
struct ISO8601FormatStyleOverview
The Date.ISO8601FormatStyle type generates and parses string representations of dates following the ISO-8601 standard, like 2024-04-01T12:34:56.789Z. Use this type to create ISO-8601 representations of dates and create dates from text strings in ISO 8601 format. For other formatting conventions, like human-readable, localized date formats, use Date.FormatStyle.
Instance modifier methods applied to an ISO-8601 format style customize the formatted output, as the following example illustrates.
let now = Date()
print(now.formatted(Date.ISO8601FormatStyle().dateSeparator(.dash)))
// 2021-06-21T211015ZUse the static factory property FormatStyle/iso8601 to create an instance of Date.ISO8601FormatStyle. Then apply instance modifier methods to customize the format, as in the example below.
let meetNow = Date()
let formatted = meetNow.formatted(.iso8601
.year()
.month()
.day()
.timeZone(separator: .omitted)
.time(includingFractionalSeconds: true)
.timeSeparator(.colon)
) // "2022-06-10T12:34:56.789Z"
Topics
Creating an ISO 8601 Format Style
Modifying an ISO 8601 Format Style
Modifying Dates in an ISO 8601 Format Style
Modifying Times in an ISO 8601 Format Style
Comparing ISO 8601 Format Styles
Supporting Types
Date.ISO8601FormatStyle.DateSeparatorDate.ISO8601FormatStyle.DateTimeSeparatorDate.ISO8601FormatStyle.TimeSeparatorDate.ISO8601FormatStyle.TimeZoneSeparator