<!--
{
  "availability" : [
    "iOS: 16.0.0 -",
    "iPadOS: 16.0.0 -",
    "macCatalyst: 16.0.0 -",
    "macOS: 13.0.0 -",
    "tvOS: 16.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 9.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/RegexComponent/dateTime(date:time:locale:timeZone:calendar:)",
  "metadataVersion" : "0.1.0",
  "role" : "Type Method",
  "symbol" : {
    "kind" : "Type Method",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:17_StringProcessing14RegexComponentP10FoundationAD4DateV13ParseStrategyVRszrlE8dateTime0I04time6locale0K4Zone8calendarAhF11FormatStyleV0fP0V_AP0jP0VAD6LocaleVAD0jM0VAD8CalendarVSgtFZ"
  },
  "title" : "dateTime(date:time:locale:timeZone:calendar:)"
}
-->

# dateTime(date:time:locale:timeZone:calendar:)

Creates a regex component that matches a localized date and time string, capturing it as a Foundation date.

```
static func dateTime(date: Date.FormatStyle.DateStyle, time: Date.FormatStyle.TimeStyle, locale: Locale, timeZone: TimeZone, calendar: Calendar? = nil) -> Date.ParseStrategy
```

## Parameters

`date`

A <doc://com.apple.documentation/documentation/Foundation/Date/FormatStyle/DateStyle> to use when matching date substrings. Pass <doc://com.apple.documentation/documentation/Foundation/Date/FormatStyle/DateStyle/omitted> to match a time-only substring.

`time`

A <doc://com.apple.documentation/documentation/Foundation/Date/FormatStyle/TimeStyle> to use when matching time substrings. Pass <doc://com.apple.documentation/documentation/Foundation/Date/FormatStyle/TimeStyle/omitted> to match a date-only substring.

`locale`

The locale to use when matching date substrings. Matching uses this locale to evaluate the order of date components. It also uses the locale’s language for date format styles that use words.

`timeZone`

The time zone to use when capturing the date and time values. The regex component ignores this parameter if the source string contains a time zone substring that the format style supports.

`calendar`

The calendar to use when matching date substrings. If `nil`, matching uses the default calendar of the specified `locale`.

## Return Value

A `RegexComponent` that matches date substrings as Foundation <doc://com.apple.documentation/documentation/Foundation/Date> instances.

## Discussion

This method matches date substrings in accordance with the formatting of Foundation’s <doc://com.apple.documentation/documentation/Foundation/Date/FormatStyle>. If your source contains only a date or a time, use the `.omitted` value for the `date` or `time` style parameter to ignore the missing part.

The following example creates a [`Regex`](/documentation/Swift/Regex) that matches a date and time formatted with the <doc://com.apple.documentation/documentation/Foundation/Date/FormatStyle/DateStyle/numeric> style in the `en_US` locale. It then matches this regex against a source string containing a date with this format, some whitespace, a substring, more whitespace, and a currency value.

```
let enUSLocale = Locale(languageCode: .english, languageRegion: .unitedStates)
let source = "7/31/2022, 5:15:12 PM  Lemon-lime slushie      $1.99"
let matcher = Regex {
    Capture {
        One(.dateTime(date: .numeric,
                      time: .standard,
                      locale: enUSLocale,
                      timeZone: TimeZone(identifier: "PST")!))
    }
    OneOrMore(.horizontalWhitespace)
    OneOrMore(.any)
    OneOrMore(.horizontalWhitespace)
    One(.localizedCurrency(code: Locale.Currency("USD"),
                           locale: enUSLocale))
}

guard let match = source.firstMatch(of: matcher) else { return }
let date = match.1 // date == Jul 31, 2022 at 5:15 PM PST
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)