SystemFormatStyle.Timer
A format style that displays a countdown or count-up timer within a bounded time interval.
Declaration
struct TimerOverview
Timer produces a time-pattern output (like 4:32 or 1:23:45) that progresses between zero and the interval’s total duration. Unlike SystemFormatStyle.Stopwatch, a timer operates within a defined start and end date and can count in either direction.
let start = Date.now
let end = start.addingTimeInterval(600)
// Countdown from 10:00 to 0:00
Text(.currentDate, format: .timer(countingDownIn: start..<end))
// Count up from 0:00 to 10:00
Text(.currentDate, format: .timer(countingUpIn: start..<end))Countdown behavior
A countdown timer starts at the total interval duration and decreases to zero:
// 5-minute countdown
let start = Date.now
let end = start.addingTimeInterval(300)
Text(.currentDate, format: .timer(countingDownIn: start..<end))Elapsed since start | Output |
|---|---|
0 seconds |
|
1 minute |
|
4 min, 30 sec |
|
5 minutes |
|
Count-up behavior
A count-up timer starts at zero and increases toward the total interval duration:
// 1-hour count-up
let start = Date.now
let end = start.addingTimeInterval(3600)
Text(.currentDate, format: .timer(countingUpIn: start..<end))Elapsed since start | Output |
|---|---|
0 seconds |
|
30 seconds |
|
59 min, 59 sec |
|
1 hour |
|
Hours display
When showsHours is true (the default), the hours field appears once the displayed value reaches one hour. The transition between formats happens cleanly:
// Countdown transition at the 1-hour boundary:
// "1:00:00" -> "59:59" -> "59:58" -> ...When showsHours is false, minutes accumulate beyond 60:
.timer(countingDownIn: start..<end, showsHours: false)
// Output for 90 minutes remaining: "90:00"Precision control
The maxPrecision parameter determines the smallest displayed unit:
// Default (1 second): "4:32"
.timer(countingDownIn: start..<end)
// Minute precision: shows "5 minutes", "4 minutes", etc.
.timer(countingDownIn: start..<end, maxPrecision: .seconds(60))