SystemFormatStyle.Stopwatch
A format style that displays elapsed time as a precision stopwatch counting up from zero.
Declaration
struct StopwatchOverview
Stopwatch produces output in a time-pattern format that includes hundredths of a second by default, making it suitable for use cases such as athletic timing where sub-second precision matters.
let startDate = Date.now
Text(.currentDate, format: .stopwatch(startingAt: startDate))
// Output: "00:05.23" (after 5.23 seconds)Output format progression
The stopwatch automatically adds an hours field when the elapsed time reaches one hour:
Elapsed | Output |
|---|---|
Under 1 min |
|
Under 1 hour |
|
1 hour or more |
|
Over 25 hours |
|
Adjusting precision
Control the displayed precision with the maxPrecision parameter:
// Whole-second precision (no fractional digits)
.stopwatch(startingAt: start, maxPrecision: .seconds(1))
// Output: "01:23"
// Tenth-of-a-second precision (one fractional digit)
.stopwatch(startingAt: start, maxPrecision: .milliseconds(100))
// Output: "01:23.4"
// Minute-only precision
.stopwatch(startingAt: start, maxPrecision: .seconds(60))
// Output: "1 hour" or "23 minutes"Controlling field count
The maxFieldCount parameter limits how many fields appear:
// At 1 hour, 23 minutes, 45.67 seconds elapsed:
// maxFieldCount: 4 (default): "01:23:45.67"
// maxFieldCount: 3: "01:23:45"
// maxFieldCount: 2: "1:23" (hours and minutes only)