Skip to content

Type Method_StringProcessing

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

iOS 16.0+ iPadOS 16.0+ Mac Catalyst 16.0+ macOS 13.0+ tvOS 16.0+ watchOS 9.0+
Creates a regex component to match a localized date and time string and capture the string as a Date. The date string is expected to follow the format of what Date.FormatStyle(date:time:locale:calendar:) produces.
static func dateTime(date: Date.FormatStyle.DateStyle, time: Date.FormatStyle.TimeStyle, locale: Locale, timeZone: TimeZone, calendar: Calendar? = nil) -> Date.ParseStrategy

Parameters

date

The style that describes the date part of the string. For example, .numeric matches “10/21/2015”, and .abbreviated matches “Oct 21, 2015” as October 21, 2015 in the en_US locale.

time

The style that describes the time part of the string.

locale

The locale of the string to be matched.

timeZone

The time zone to create the matched date with. Ignored if the string contains a time zone and matches the specified style.

calendar

The calendar with which to interpret the date string. If set to nil, the default calendar of the specified locale is used.

Return Value

A RegexComponent to match a localized date string.

Discussion

Note

If the string contains a time zone and matches the specified style, then the timeZone argument is ignored. For example, “Oct 21, 2015 4:29:24 PM PDT” matches .dateTime(date: .abbreviated, time: .complete, ...) and is captured as October 13, 2022, 20:29:24 PDT regardless of the timeZone value.