Skip to content

StructureFoundationEssentials

Date.RelativeFormatStyle

iOS 15.0+ iPadOS 15.0+ Mac Catalyst 15.0+ macOS 12.0+ tvOS 15.0+ watchOS 8.0+
A format style that forms locale-aware string representations of a relative date or time.
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 Presentation
    A type that represents the style to use when formatting relative dates, such as “1 week ago” or “last week”.
  • struct UnitsStyle
    A type that represents the style to use when formatting the units of relative dates.

Initializers

Instance Properties

Instance Methods

Type Aliases

Relationships

Conforms To

  • FormatStyle
  • Swift.Copyable
  • Swift.Decodable
  • Swift.Encodable
  • Swift.Equatable
  • Swift.Escapable
  • Swift.Hashable
  • Swift.Sendable
  • Swift.SendableMetatype