Structure

Date.RelativeFormatStyle

A format style that forms locale-aware string representations of a relative date or time.

Declaration 宣言

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. format(_:)インスタンスメソッドは、ある文字列をこの提供された相対日付から生成します。 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 話題

Creating a Relative Date Format Style

Modifying a Relative Date Format Style

Formatting a Relative Date

Encoding and Decoding Relative Date Format Styles

Hashing Relative Date Format Styles

Comparing Relative Date Format Styles

Supporting Types 支援を行う型

Relationships 関係

Conforms To 次に準拠