Instance Method
format(_:)
iOS 15.0+
iPadOS 15.0+
Mac Catalyst 15.0+
macOS 12.0+
tvOS 15.0+
watchOS 8.0+
Creates a locale-aware string representation from a relative date value.
func format(_ destDate: Date) -> String
Parameters
destDateThe date to format.
Return Value
A string representation of the relative date.
Discussion
Once you create a style, you can use it to format relative dates multiple times.
if let pastWeek = Calendar.current.date(byAdding: .day, value: -7, to: Date()),
let pastDay = Calendar.current.date(byAdding: .day, value: -1, to: Date()) {
let formatStyle = Date.RelativeFormatStyle(
presentation: .named,
unitsStyle: .spellOut,
locale: Locale(identifier: "en_GB"),
calendar: Calendar.current,
capitalizationContext: .beginningOfSentence)
formatStyle.format(pastDay) // "Yesterday"
formatStyle.format(pastWeek) // "Last week"
}