verbatim(_:locale:timeZone:calendar:)
Returns a style for formatting a date with an explicitly-specified style.
Declaration
static func verbatim(_ format: Date.FormatString, locale: Locale? = nil, timeZone: TimeZone, calendar: Calendar) -> Date.VerbatimFormatStyleParameters
- format:
A 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. The format style zero-pads the month and day components. This style isn’t localized — while this format string mimicks en_US conventions, it uses this format in any locale, ignoring locale-apporpriate 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