Type Method 型メソッド

relative(presentation:unitsStyle:)

Returns a relative date format style based on the provided presentation and unit style.

Declaration 宣言

static func relative(presentation: Presentation, unitsStyle: UnitsStyle = .wide) -> Date.RelativeFormatStyle

Parameters パラメータ

presentation

The style to use when describing a relative date, for example “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”。

Return Value 戻り値

A relative date format style customized with the specified presentation and unit styles.

Discussion 議論

Use this convenient static factory method 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)) // "1 week ago"
    past.formatted(.relative(presentation: .named)) // "last week"


    past.formatted(.relative(presentation: .named, unitsStyle: .wide)) // "last week"
    past.formatted(.relative(presentation: .named, unitsStyle: .narrow)) // "last wk."
    past.formatted(.relative(presentation: .named, unitsStyle: .abbreviated)) // "last wk."
    past.formatted(.relative(presentation: .named, unitsStyle: .spellOut)) // "last week"
    past.formatted(.relative(presentation: .numeric, unitsStyle: .wide)) // "1 week ago"
    past.formatted(.relative(presentation: .numeric, unitsStyle: .narrow)) // "1 wk. ago"
    past.formatted(.relative(presentation: .numeric, unitsStyle: .abbreviated)) // "1 wk. ago"
    past.formatted(.relative(presentation: .numeric, unitsStyle: .spellOut)) // "one week ago"
}

See Also 参照

Modifying a Relative Date Format Style