The date to format. 書式設定する日付。
Instance Method
インスタンスメソッド
format(_:)
Creates a locale-aware string representation from a relative date value.
ロケール対応文字列表現を相対日付値から作成します。
Availability 有効性
- iOS 15.0+
- iPadOS 15.0+
- macOS 12.0+
- Mac Catalyst 15.0+
- tvOS 15.0+
- watchOS 8.0+
- Xcode 13.0+
Technology
- Foundation ファウンデーション
Declaration 宣言
Parameters パラメータ
destDate
Return Value 戻り値
A string representation of the relative date.
Discussion 議論
The format(_:)
instance method generates a string from the provided relative date.
format(_:)
インスタンスメソッドは、ある文字列をこの提供された相対日付から生成します。
Once you create a style, you can use it to format 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()) {
if 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"
}
}