Type Property 型プロパティ

interval

A convenience factory variable to use as a base for custom date interval format styles. あつらえの日付間隔書式設定スタイルそれらの基盤として使うための便宜ファクトリ変数。

Declaration 宣言

static var interval: Date.IntervalFormatStyle { get }

Discussion 議論

Customize the date interval format style using modifier syntax to apply specific date and time formats, as in the following example: 日付間隔書式設定スタイルを修飾子構文を使ってカスタマイズすることで、特定の日付と時刻書式設定を適用してください、以下の例でのように:


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


    // Create a Range<Date>.
    let weekBefore = sevenDaysBeforeToday..<today


    let localeArray = ["en_US", "sv_SE", "en_GB", "th_TH", "fr_BE"]
    for localeID in localeArray {
        // Call the .formatted method on a Range<Date> and pass in an instance of Date.IntervalFormatStyle.
        print(weekBefore.formatted(.interval
                 .day()
                 .month(.wide)
                 .weekday(.short)
                 .hour(.conversationalTwoDigits(amPM: .wide))
                 .locale(Locale(identifier: localeID))))
    }
}
// We, February 3, 3 PM – We, February 10, 3 PM
// on 3 februari 15 – on 10 februari 15
// We 3 February, 15 – We 10 February, 15
// พ. 3 กุมภาพันธ์ 15 – พ. 10 กุมภาพันธ์ 15
// me 3 février, 15 h – me 10 février, 15 h

The default format styles are numeric date format and shortened time format, as in the following example: 省略時の書式設定スタイルは、numeric日付書式設定とshortened時刻書式設定です、以下の例でのように:


let today = Date.now
if let sevenDaysBeforeToday = Calendar.current.date(byAdding: .day, value: -7, to: today) {
    // Create a Range<Date>.
    let weekBefore = sevenDaysBeforeToday..<today
    print(weekBefore.formatted(.interval))
}
// 2/25/21, 12:58 PM – 3/4/21, 12:58 PM

See Also 参照

Creating a Date Interval Format Style 日付間隔書式設定スタイルを作成する