Initializer

init(presentation:unitsStyle:locale:calendar:capitalizationContext:)

Creates a relative date format style with the specified presentation, units, locale, calendar, and capitalization context.

Declaration 宣言

init(presentation: Date.RelativeFormatStyle.Presentation = .numeric, unitsStyle: Date.RelativeFormatStyle.UnitsStyle = .wide, locale: Locale = .autoupdatingCurrent, calendar: Calendar = .autoupdatingCurrent, capitalizationContext: FormatStyleCapitalizationContext = .unknown)

Parameters パラメータ

presentation

The style to use when describing a relative date, such as “1 day ago” or “yesterday”.

unitsStyle

The style to use when formatting the quantity or the name of the unit, such as “1 day ago” or “one day ago”. 単位の量または名前を書式設定する時に使う様式、たとえば “1 day ago” または “one day ago”。

locale

The locale to use when formatting the relative date.

calendar

The calendar to use when formatting the relative date.

capitalizationContext

The capitalization context to use when formatting the relative date.

Discussion 議論

The following example creates a format style applied to a relative date to create a string representation.


if let past = Calendar.current.date(byAdding: .day, value: -7, to: Date()) {


    let formatStyle = Date.RelativeFormatStyle(
        presentation: .named,
        unitsStyle: .abbreviated,
        locale: Locale(identifier: "en_US"),
        calendar: Calendar.current,
        capitalizationContext: .beginningOfSentence)
    
    print(past.formatted(formatStyle)) // "Last wk."
}