I'm trying to use the DateComponentsFormatter with the new Foundation formatters. To format a date, I can do something like this:
Date.now.formatted(.dateTime.hour().minute().second())
// 5:03:17 PM
However, I'm trying to use this new API for using the DateComponentsFormatter:
let duration: TimeInterval = 0
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .positional
formatter.allowedUnits = [.minute, .second]
formatter.zeroFormattingBehavior = [.pad]
let formattedDuration = formatter.string(from: 0)
// 00:00
How can I use the new Foundation formatters API for DateComponentsFormatter to achieve this?
AFAIK the formatted method for a time interval only works with
Durationobjects which needs to be initialized with atimeval:or
You can also use its static methods for initialization:
And if you need further customization: