StructureFoundationEssentials
Date.ISO8601FormatStyle
iOS 15.0+
iPadOS 15.0+
Mac Catalyst 15.0+
macOS 12.0+
tvOS 15.0+
watchOS 8.0+
A type that converts between dates and their ISO-8601 string representations.
struct ISO8601FormatStyle
Overview
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-21T211015Z
Use 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
Operators
Initializers
init(dateSeparator: Date.ISO8601FormatStyle.DateSeparator, dateTimeSeparator: Date.ISO8601FormatStyle.DateTimeSeparator, timeSeparator: Date.ISO8601FormatStyle.TimeSeparator, timeZoneSeparator: Date.ISO8601FormatStyle.TimeZoneSeparator, includingFractionalSeconds: Bool, timeZone: TimeZone)init(dateSeparator: Date.ISO8601FormatStyle.DateSeparator, dateTimeSeparator: Date.ISO8601FormatStyle.DateTimeSeparator, timeZone: TimeZone)Creates an instance using the provided date separator, date and time components separator, and time zone.init(from: any Decoder) throws
Instance Properties
var dateSeparator: Date.ISO8601FormatStyle.DateSeparatorThe character used to separate the components of a date.var dateTimeSeparator: Date.ISO8601FormatStyle.DateTimeSeparatorThe character used to separate the date and time components of an ISO 8601 string representation of a date.var includingFractionalSeconds: BoolIf set, the style includes fractional seconds when formatting. Before Swift 6.2, if true when parsing, fractional seconds must be present. If false when parsing, fractional seconds must not be present. After Swift 6.2, fractional seconds may be present in the String regardless of the setting of this property.var timeSeparator: Date.ISO8601FormatStyle.TimeSeparatorvar timeZone: TimeZoneThe time zone to use to create and parse date representations.var timeZoneSeparator: Date.ISO8601FormatStyle.TimeZoneSeparator
Instance Methods
func dateSeparator(Date.ISO8601FormatStyle.DateSeparator) -> Date.ISO8601FormatStyleModifies the ISO 8601 date format style to use the specified date separator.func dateTimeSeparator(Date.ISO8601FormatStyle.DateTimeSeparator) -> Date.ISO8601FormatStyleSets the character that separates the date and time components.func day() -> Date.ISO8601FormatStyleModifies the ISO 8601 date format style to include the day in the formatted output.func encode(to: any Encoder) throwsfunc hash(into: inout Hasher)func month() -> Date.ISO8601FormatStyleModifies the ISO 8601 date format style to include the month in the formatted output.func time(includingFractionalSeconds: Bool) -> Date.ISO8601FormatStyleModifies the ISO 8601 date format style to include the time in the formatted output.func timeSeparator(Date.ISO8601FormatStyle.TimeSeparator) -> Date.ISO8601FormatStyleModifies the ISO 8601 date format style to use the specified time separator.func timeZone(separator: Date.ISO8601FormatStyle.TimeZoneSeparator) -> Date.ISO8601FormatStyleModifies the ISO 8601 date format style to include the time zone in the formatted output.func timeZoneSeparator(Date.ISO8601FormatStyle.TimeZoneSeparator) -> Date.ISO8601FormatStyleModifies the ISO 8601 date format style to use the specified time zone separator.func weekOfYear() -> Date.ISO8601FormatStyleModifies the ISO 8601 date format style to include the week of the year in the formatted output.func year() -> Date.ISO8601FormatStyleModifies the ISO 8601 date format style to include the year in the formatted output.
Enumerations
enum DateSeparatorA type describing the character separating year, month, and day components of a date in an ISO 8601 date format.enum DateTimeSeparatorType describing the character separating the date and time components of a date in an ISO 8601 date format.enum TimeSeparatorType describing the character separating the time components of a date in an ISO 8601 date format.enum TimeZoneSeparatorA type describing the character separating the time and time zone of a date in an ISO 8601 date format.
Default Implementations
Relationships
Conforms To
FormatStyleParseStrategyParseableFormatStyleSwift.CopyableSwift.DecodableSwift.EncodableSwift.EquatableSwift.EscapableSwift.HashableSwift.SendableSwift.SendableMetatype_StringProcessing.CustomConsumingRegexComponent_StringProcessing.RegexComponent