StructureFoundationEssentials
Date.RelativeFormatStyle
struct RelativeFormatStyle
Overview
Use the strings that the format style produces, such as “1 hour ago”, “in 2 weeks”, “yesterday”, and “tomorrow” as standalone strings. Embedding them in other strings may not be grammatically correct.
Express relative date formats in either numeric or named styles. For example:
if let past = Calendar.current.date(byAdding: .day, value: -7, to: Date()) {
var formatStyle = Date.RelativeFormatStyle()
formatStyle.presentation = .numeric
past.formatted(formatStyle) // "1 week ago"
formatStyle.presentation = .named
past.formatted(formatStyle) // "last week"
}
Use the convenient static factory method relative(presentation:unitsStyle:) to shorten the syntax when applying presentation and units style modifiers to customize the format. For example:
if let past = Calendar.current.date(byAdding: .day, value: 7, to: Date()) {
past.formatted(.relative(presentation: .numeric)) // "in 1 week"
past.formatted(.relative(presentation: .named)) // "next week"
past.formatted(.relative(presentation: .named, unitsStyle: .wide)) // "next week"
past.formatted(.relative(presentation: .named, unitsStyle: .narrow)) // "next wk."
past.formatted(.relative(presentation: .named, unitsStyle: .abbreviated)) // "next wk."
past.formatted(.relative(presentation: .named, unitsStyle: .spellOut)) // "next week"
past.formatted(.relative(presentation: .numeric, unitsStyle: .wide)) // "in 1 week"
past.formatted(.relative(presentation: .numeric, unitsStyle: .narrow)) // "in 1 wk."
past.formatted(.relative(presentation: .numeric, unitsStyle: .abbreviated)) // "in 1 wk."
past.formatted(.relative(presentation: .numeric, unitsStyle: .spellOut)) // "in one week"
}
The format(_:) instance method generates a string from the provided relative date. Once you create a style, you can use it to format relative dates multiple times.
The following example applies a format style repeatedly to produce string representations of relative dates:
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"
}
Topics
Structures
struct PresentationA type that represents the style to use when formatting relative dates, such as “1 week ago” or “last week”.struct UnitsStyleA type that represents the style to use when formatting the units of relative dates.
Initializers
init(allowedFields: Set<Date.RelativeFormatStyle.Field>, presentation: Date.RelativeFormatStyle.Presentation, unitsStyle: Date.RelativeFormatStyle.UnitsStyle, locale: Locale, calendar: Calendar, capitalizationContext: FormatStyleCapitalizationContext)init(from: any Decoder) throwsinit(presentation: Date.RelativeFormatStyle.Presentation, unitsStyle: Date.RelativeFormatStyle.UnitsStyle, locale: Locale, calendar: Calendar, capitalizationContext: FormatStyleCapitalizationContext)Creates a relative date format style with the specified presentation, units, locale, calendar, and capitalization context.
Instance Properties
var allowedFields: Set<Date.RelativeFormatStyle.Field>The fields that can be used in the formatted output.var calendar: CalendarThe calendar to use when formatting relative dates.var capitalizationContext: FormatStyleCapitalizationContextThe capitalization context to use when formatting the relative dates.var locale: LocaleThe locale to use when formatting the relative date.var presentation: Date.RelativeFormatStyle.PresentationSpecifies the style to use when describing a relative date, such as “1 day ago” or “yesterday”.var unitsStyle: Date.RelativeFormatStyle.UnitsStyleThe style to use when formatting the quantity or the name of the unit, such as “1 day ago” or “one day ago”.
Instance Methods
func format(Date) -> StringCreates a locale-aware string representation from a relative date value.func locale(Locale) -> Date.RelativeFormatStyleModifies the relative date format style to use the specified locale.
Type Aliases
Relationships
Conforms To
FormatStyleSwift.CopyableSwift.DecodableSwift.EncodableSwift.EquatableSwift.EscapableSwift.HashableSwift.SendableSwift.SendableMetatype