interval
A style for formatting a date interval.
Declaration
static var interval: Date.IntervalFormatStyle { get }Discussion
Use this type property when the call point allows the use of Date.IntervalFormatStyle. You typically do this when calling the formatted(_:) method of a Range<Date>.
The folllowing example uses interval to create a date interval string with specific styling of the day, month, and weekday components, omitting the year and time.
if let today = Calendar.current.date(byAdding: .day, value: -120, to: Date()),
let thirtyDaysBeforeToday = Calendar.current.date(byAdding: .day, value: -30, to: today) {
// today: June 5, 2023
// thirtyDaysBeforeToday: May 6, 2023
// Create a Range<Date>.
let last30days = thirtyDaysBeforeToday..<today
let formatted = last30days.formatted(
.interval
.day()
.month(.wide)
.weekday(.abbreviated)
) // "Sat, May 6 – Mon, June 5"
}