format time in hh:mm:ss given a certain amount of secconds in swift

main.swift
func formatTime(timeInSeconds: Int) -> String {
    let hours = timeInSeconds / 3600
    let minutes = (timeInSeconds % 3600) / 60
    let seconds = (timeInSeconds % 3600) % 60
    
    return String(format: "%02d:%02d:%02d", hours, minutes, seconds)
}

// Example usage
let timeInSeconds = 3785
let formattedTime = formatTime(timeInSeconds: timeInSeconds)
print(formattedTime) // Output: 01:03:05
398 chars
13 lines

related categories

gistlibby LogSnag