Skip to content

Type MethodFoundationEssentials

verbatim(_:locale:timeZone:calendar:)

iOS 16.0+ iPadOS 16.0+ Mac Catalyst 16.0+ macOS 13.0+ tvOS 16.0+ watchOS 9.0+
Returns a style for formatting a date with an explicitly-specified style.
static func verbatim(_ format: Date.FormatString, locale: Locale? = nil, timeZone: TimeZone, calendar: Calendar) -> Date.VerbatimFormatStyle

Parameters

format

A Date.FormatString that provides the explicit components and their respective styles to use when formatting a date.

locale

The locale to use when formatting. Defaults to nil.

timeZone

The time zone to use when formatting.

calendar

The calendar to use when formatting.

Return Value

A date format style that uses the provided format string and timekeeping parameters.

Discussion

Use this format style only when you need to produce or parse an exact format, such as when working with programmatically-produced date strings. For formatting dates that people read, use dateTime to get a localized Date.FormatStyle instead. To use the ISO-8601 standard, use FormatStyle/iso8601 to get a Date.ISO8601FormatStyle.

Use the dot-notation form of this type method when the call point allows the use of Date.VerbatimFormatStyle. You typically do this when calling the formatted(_:) method of Date.

The following example formats the current date with a verbatim format that uses a two-digit month, two-digit day, and default-digits year, separated by slashes. This style isn’t localized — it uses this format in any locale, ignoring locale-appropriate conventions.

let date = Date()
let formatted = date.formatted(
    .verbatim("\(month: .twoDigits)/\(day: .twoDigits)/\(year: .defaultDigits)" as Date.FormatString,
              locale: .autoupdatingCurrent,
              timeZone: .current,
              calendar: .current)) // 12/05/2022