a function that gets a total amount of seconds in double and returns the hour, minutes, seconds in swift

main.swift
func getTimeComponents(totalSeconds: Double) -> (hour: Int, minute: Int, second: Int) {
    let hours = Int(totalSeconds / 3600)
    let minutes = Int((totalSeconds.truncatingRemainder(dividingBy: 3600)) / 60)
    let seconds = Int(totalSeconds.truncatingRemainder(dividingBy: 60))
    
    return (hours, minutes, seconds)
}

// Usage
let totalSeconds = 3665.0
let time = getTimeComponents(totalSeconds: totalSeconds)
print("Hours: \(time.hour), Minutes: \(time.minute), Seconds: \(time.second)")
498 chars
13 lines
  • This function getTimeComponents takes the total number of seconds as input and calculates the equivalent hours, minutes, and seconds.
  • The function then returns a tuple of integers representing the hour, minute, and second components respectively.
  • You can call the function by passing the total seconds as an argument and then print the individual components like hours, minutes, and seconds.

related categories

gistlibby LogSnag