Date.VerbatimFormatStyle
This is part of the FormatStyle Deep Dive series
The Date.VerbatimFormatStyle
outputs a date string that bypasses the locale on your device. You use a special format string to composite a date out of parts, and can specify the calendar and time zone for the display.
Download the Xcode Playground with all examples
Sidenote: The VerbatimFormatStyle is a strange one. As of this writing, the apple docs are empty and the headers contain the following line: “Formats a
Date
using the given format.”
What cracked the code is a post on the Swift Evolution Forums about outputting 24 hour time using the new
FormatStyle
There’s no extension on FormatStyle
that lets you quickly access a VerbatimFormatStyle instance. You’re stuck creating a new instance and Extending FormatStyle
yourself.
let twosdayDateComponents = DateComponents(
year: 2022,
month: 2,
day: 22,
hour: 2,
minute: 22,
second: 22,
nanosecond: 22
)
let twosday = Calendar(identifier: .gregorian).date(from: twosdayDateComponents)!
let verbatim = Date.VerbatimFormatStyle(
format: "\(hour: .twoDigits(clock: .twentyFourHour, hourCycle: .oneBased)):\(minute: .twoDigits)",
timeZone: TimeZone.current,
calendar: .current
)
verbatim.format(twosday) // "02:22"
The format
property on the init method is of type Date.FormatString
, this uses the string interpolation system under the hood. You have access to all of the date Date.FormatStyle.Symbol
values used in the Date.FormatStyle.datTime
style.
.day()
The numerical day of the month.dayOfYear()
The numerical day of the year.era()
The era (AD/BC in the gregorian calendar for example).minute()
The numerical minute.month()
The month as a string.quarter()
The quarter.second()
The Numerical second.timeZone()
The time zone.week()
The numerical week of the month.weekday()
The weekday as a string.year()
The numerical year
Hours are different, there’s a special Date.FormatStyle.Symbol.VerbatimHour
type to represent the hour. It gives you the ability to display a 24 hour clock, instead of a 12 hour clock as well as zero pad the hours.
Attributed String Output
AttributedString
output can be had by this formatter by adding the .attributed
parameter to the FormatStyle
during use.
You can read more details in the AttributedString Output deep-dive
Download the Xcode Playground with all examples