Xcode 12.1. Swift 5
DateComponentsFormatter: A formatter that creates string representations, by using unitsStyle u can get a string as you want and mention allowedUnits. e
e.g: output for unitsStyle:: for 10000 secods
- full = "2 hours, 46 minutes, 49 seconds"
- positional = "2:46:40"
- abbreviated = "2h 46m 40s"
- spellOut = "two hours, forty-six minutes, forty seconds”
- short = "2hr,46 min,40 sec"
- brief = "2hr 46min 40sec"
Easy to use:
let time = convertSecondsToHrMinuteSec(seconds: 10000) func convertSecondsToHrMinuteSec(seconds:Int) -> String{ let formatter = DateComponentsFormatter() formatter.allowedUnits = [.hour, .minute, .second] formatter.unitsStyle = .full let formattedString = formatter.string(from:TimeInterval(seconds))! print(formattedString) return formattedString }