A type that converts between dates and their ISO-8601 string representations.
The ``Date/ISO8601FormatStyle`` type generates and parses string representations of dates following the [ISO-8601](https://www.iso.org/iso-8601-date-and-time-format.html) 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.
```swift
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.
```swift
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"
```
Members (23)
method · iOS 15.0+
weekOfYear
TypeScript
weekOfYear(): Date_ISO8601FormatStyle
Swift declaration
func weekOfYear() -> Date.ISO8601FormatStyle
Modifies the ISO 8601 date format style to include the week of the year in the formatted output.
When the format style includes the week of year, the output represents the day
as the ordinal day of the week.
- Returns: An ISO 8601 date format style modified to include the week of the year.
Creates an instance using the provided date separator, date and time components separator, and time zone.
Possible values of `dateSeparator` are `dash` and `omitted`.
Possible values of `dateTimeSeparator` are `space` and `standard`.
```swift
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
```
- Parameters:
- dateSeparator: The separator character used between the year, month, and day.
- dateTimeSeparator: The separator character used between the date and time components.
- timeZone: The time zone used to create the string representation of the date.
get · iOS 15.0+
dateSeparator
TypeScript
get dateSeparator(): Date_ISO8601FormatStyle_DateSeparator
Swift declaration
var dateSeparator: Date.ISO8601FormatStyle.DateSeparator { get }
The character used to separate the components of a date.
Modifies the ISO 8601 date format style to use the specified date separator.
Possible values are ``DateSeparator/dash`` and ``DateSeparator/omitted``.
The default is ``DateSeparator/omitted``.
- Parameter separator: The character used to separate the year, month, and day in a date.
- Returns: An ISO 8601 date format style modified to include the specified date separator style.
get · iOS 15.0+
parseStrategy
TypeScript
get parseStrategy(): Date_ISO8601FormatStyle
Swift declaration
var parseStrategy: Date.ISO8601FormatStyle { get }
The strategy used to parse a string into a date.
get · iOS 15.0+
timeSeparator
TypeScript
get timeSeparator(): Date_ISO8601FormatStyle_TimeSeparator
Swift declaration
var timeSeparator: Date.ISO8601FormatStyle.TimeSeparator { get }
Modifies the ISO 8601 date format style to use the specified time separator.
Possible values are ``TimeSeparator/colon`` and ``TimeSeparator/omitted``.
The default is ``TimeSeparator/omitted``.
- Parameter separator: The character used to separate the hour and minute in a date.
- Returns: An ISO 8601 date format style modified to include the specified time separator style.
get · iOS 15.0+
dateTimeSeparator
TypeScript
get dateTimeSeparator(): Date_ISO8601FormatStyle_DateTimeSeparator
Swift declaration
var dateTimeSeparator: Date.ISO8601FormatStyle.DateTimeSeparator { get }
The character used to separate the date and time components of an ISO 8601 string representation of a date.
Sets the character that separates the date and time components.
Possible values are ``DateTimeSeparator/space`` and ``DateTimeSeparator/standard``.
The default is ``DateTimeSeparator/standard``.
- Parameter separator: The character used to separate the date and time components.
- Returns: An ISO 8601 date format style with the provided date and time component separator.
get · iOS 15.0+
timeZoneSeparator
TypeScript
get timeZoneSeparator(): Date_ISO8601FormatStyle_TimeZoneSeparator
Swift declaration
var timeZoneSeparator: Date.ISO8601FormatStyle.TimeZoneSeparator { get }
Modifies the ISO 8601 date format style to use the specified time zone separator.
Possible values are ``TimeZoneSeparator/colon`` and ``TimeZoneSeparator/omitted``.
- Parameter separator: The character used to separate the time and time zone in a date.
- Returns: An ISO 8601 date format style modified to include the specified time zone separator style.
get · iOS 15.0+
includingFractionalSeconds
TypeScript
get includingFractionalSeconds(): boolean
Swift declaration
var includingFractionalSeconds: Bool { get }
If 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.
method · iOS 15.0+
day
TypeScript
day(): Date_ISO8601FormatStyle
Swift declaration
func day() -> Date.ISO8601FormatStyle
Modifies the ISO 8601 date format style to include the day in the formatted output.
If `month()` isn't included in the format and `day()` is, the format represents
the day as the ordinal date. The default ``Date/ISO8601FormatStyle`` includes the day.
- Returns: An ISO 8601 date format style modified to include the day.
Modifies the ISO 8601 date format style to include the time in the formatted output.
The default ``Date/ISO8601FormatStyle`` includes the time but not the fractional seconds.
- Parameter includingFractionalSeconds: Specifies whether the format style includes the fractional component of the seconds.
- Returns: An ISO 8601 date format style modified to include the time.
method · iOS 15.0+
year
TypeScript
year(): Date_ISO8601FormatStyle
Swift declaration
func year() -> Date.ISO8601FormatStyle
Modifies the ISO 8601 date format style to include the year in the formatted output.
The default ``Date/ISO8601FormatStyle`` includes the year.
- Returns: An ISO 8601 date format style modified to include the year.
method · iOS 15.0+
month
TypeScript
month(): Date_ISO8601FormatStyle
Swift declaration
func month() -> Date.ISO8601FormatStyle
Modifies the ISO 8601 date format style to include the month in the formatted output.
If `month()` isn't included in the format but `day()` is, the format represents
the day as the ordinal date. The default ``Date/ISO8601FormatStyle`` includes the month.
- Returns: An ISO 8601 date format style modified to include the month.
method · iOS 15.0+
parse
TypeScript
parse(value: string): globalThis.Date
Swift declaration
func parse(_ value: String) throws -> Date
Parses a string into a date.
This method attempts to parse a provided string into an instance of
date using the source date format style. The function throws an error
if it can't parse the input string into a date instance.
```swift
let birthdayFormatStyle = Date.ISO8601FormatStyle()
.dateSeparator(.dash)
.timeSeparator(.colon)
.year()
.month()
.day()
.time(includingFractionalSeconds: false)
let yourBirthdayString = "2021-02-17T14:33:25"
let yourBirthday = try? birthdayFormatStyle.parse(yourBirthdayString)
// Feb 17, 2021 at 8:33 AM
```
- Parameter value: The string to parse.
- Returns: An instance of `Date` parsed from the input string.
method · iOS 15.0+
format
TypeScript
format(value: globalThis.Date): string
Swift declaration
func format(_ value: Date) -> String
Creates a locale-aware ISO 8601 string representation from a date value.
Once you create a style, you can use it to format dates multiple times.
- Parameter value: The date to format.
- Returns: A string ISO 8601 representation of the date.
Modifies the ISO 8601 date format style to include the time zone in the formatted output.
The default ``Date/ISO8601FormatStyle`` doesn't include the time zone.
- Parameter separator: The character used to separate the time and time zone in a date.
- Returns: An ISO 8601 date format style modified to include the time zone.
get · iOS 15.0+
timeZone
TypeScript
get timeZone(): TimeZone
Swift declaration
var timeZone: TimeZone { get set }
The time zone to use to create and parse date representations.
set · iOS 15.0+
timeZone
TypeScript
set timeZone(value: TimeZone)
Swift declaration
var timeZone: TimeZone { get set }
The time zone to use to create and parse date representations.
Processes the input string within the specified bounds, beginning at the given index, and returns the end position of the match and the produced output.
Don't call this method directly. Regular expression matching and capture
calls it automatically when matching substrings.
- Parameters:
- input: An input string to match against.
- index: The index within `input` at which to begin searching.
- bounds: The bounds within `input` in which to search.
- Returns: The upper bound where the match terminates and a matched instance, or `nil` if there isn't a match.