I am using the following DateComponentsFormatter in Swift:
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .positional
formatter.allowedUnits = [.hour, .minute, .second]
formatter.zeroFormattingBehavior = [.default]
This produces results like 2:41 (for 2 minutes and 41 seconds) and 08 (for 8 seconds). I would, however, like to keep at least one digit in the Minutes place, returning 0:08 instead of 08. Is this possible with the DateComponentsFormatter? I would prefer a solution that returns a localized result.
Yes. This can be easily accomplished adding a ternary conditional based on the time interval when setting the date components formatter allowed units:
Xcode 11 • Swift 5.1
Usage