Contents

iso8601

A regex component that matches a default ISO 8601-formatted date string, capturing it as a Foundation date.

Declaration

static var iso8601: Date.ISO8601FormatStyle { get }

Discussion

This property matches date substrings in accordance with the formatting of Foundation’s Date.ISO8601FormatStyle. This matches the default format of the ISO 8601 format style.

The following example creates a Regex that matches a date formatted with the base ISO 8601 format. It then matches this regex against a source string containing a date and time with this format (using the default UTC time zone, which Z indicates), some whitespace, a substring, more whitespace, and a currency value.

let iso860Source = "2022-07-14T21:10:15Z   Lemon-lime slushie      $1.99"
let matcher = Regex {
    Capture {
        One(.iso8601)
    }
    OneOrMore(.horizontalWhitespace)
    OneOrMore(.any)
    OneOrMore(.horizontalWhitespace)
    One(.localizedCurrency(code:Locale.Currency("USD"),
                           locale:Locale(identifier: "en_US")))
}
let match = iso860Source.firstMatch(of: matcher)
let date = match?.1 // date == Jul 14, 2022 at 2:10 PM PST

See Also

Matching dates and times