I have a (somewhat?) basic question regarding time conversions in Swift.
I have an integer that I would like converted into Hours / Minutes / Second
In macOS 10.10+ / iOS 8.0+ (NS)DateComponentsFormatter
has been introduced to create a readable string.
It considers the user's locale und language.
let interval = 27005
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.hour, .minute, .second]
formatter.unitsStyle = .full
let formattedString = formatter.string(from: TimeInterval(interval))!
print(formattedString)
The available unit styles are positional
, abbreviated
, short
, full
, spellOut
and brief
.
For more information please read the documenation.