Contents

timeDuration

A style for formatting a duration expressed as a range of dates.

Declaration

static var timeDuration: Date.ComponentsFormatStyle { get }

Discussion

Use this type property when the call point allows the use of Date.FormatStyle. You typically do this when calling the formatted(_:) method of Date.

The following example creates a one hour, thirty-five minute range between two dates, then uses SELF to format this duration as a string.

let now = Date.now
let future = now.addingTimeInterval(95)
let dateRange = now..<future
let formatted = dateRange.formatted(.timeDuration) // "1:35"
XCTAssertEqual(formatted, "1:35")

To use the Swift Duration type rather than Date, use Duration.TimeFormatStyle or Duration.UnitsFormatStyle instead, and their corresponding static accessors, time(pattern:) and units(allowed:width:maximumUnitCount:zeroValueUnits:valueLength:fractionalPart:).

See Also

Applying duration styles