Created
April 6, 2023 14:46
-
-
Save nitin-agam/4a25097f2f06a085a4a314a899e654fc to your computer and use it in GitHub Desktop.
Here is an example to convert seconds in display time format.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
func formatDuration(_ durationInSeconds: Int) -> String { | |
let (hours, secondsAfterHours) = divmod(durationInSeconds, 3600) | |
let (minutes, seconds) = divmod(secondsAfterHours, 60) | |
return String(format: "%02d:%02d:%02d", hours, minutes, seconds) | |
} | |
func divmod(_ numerator: Int, | |
_ denominator: Int) -> (quotient: Int, remainder: Int) { | |
let quotient = numerator / denominator | |
let remainder = numerator % denominator | |
return (quotient, remainder) | |
} | |
let durationInSeconds = 3661 | |
let formattedDuration = formatDuration(durationInSeconds) | |
print("Duration: \(formattedDuration)") // Duration: 01:01:01 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment