I have a date from which I am calculating the elapsed time, like this:
let dateComponentsFormatter = DateComponentsFormatter()
dateComponentsFormatter.allowedUnits = [.hour, .minute, .second]
dateComponentsFormatter.unitsStyle = .positional
dateComponentsFormatter.zeroFormattingBehavior = .pad
if let timeElapsed = dateComponentsFormatter.string(from: startTime, to: Date()) {
timerLabel.text = timeElapsed
}
If five seconds has passed it pads all the zeros to make the string say 0:00:05
. However, I'm looking to make it say 0:05
for five seconds, 10:05
for 10 minutes and 5 seconds, and 1:10:05
for 1 hour, 10 minutes, and five seconds. How can I do that?