- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Swift integer conversion to Hours/Minutes/Seconds

In Swift, there are many approaches to converting an integer to time components like hours, minutes, and seconds. Every approach depends on the requirement. For example, you can use arithmetic operators to convert. Another approach is using DateComponentsFormatter class to convert an integer into time components easily.

In this article, you will see many examples of converting an integer to time components.

## Example 1

To convert an integer representing a duration in seconds to hours, minutes, and seconds, you can use the following code in Swift −

import Foundation let durationInSeconds = 3661 let hours = durationInSeconds / 3600 let minutes = (durationInSeconds % 3600) / 60 let seconds = durationInSeconds % 60 print("Duration: \(hours) hours, \(minutes) minutes, \(seconds) seconds")

## Output

Duration: 1 hours, 1 minutes, 1 seconds

This code calculates the number of hours by dividing the duration by 3600 (the number of seconds in an hour), the number of minutes by taking the remainder after dividing by 3600 and then dividing that by 60 (the number of seconds in a minute), and the number of seconds by taking the remainder after dividing by 60.

## Example 2: Converting 60 Seconds to Hours, Minutes, and Seconds

import Foundation let durationInSeconds = 60 let hours = durationInSeconds / 3600 let minutes = (durationInSeconds % 3600) / 60 let seconds = durationInSeconds % 60 print("Duration: \(hours) hours, \(minutes) minutes, \(seconds) seconds")

## Output

Duration: 0 hours, 1 minutes, 0 seconds

In this example, the duration is 60 seconds, which is equal to 0 hours, 1 minute, and 0 seconds. The code calculates these values and prints them to the console.

## Example 3: Converting Seconds Using the DateComponentsFormatter Class

Here is an alternative approach using the DateComponentsFormatter class in Swift −

import Foundation let durationInSeconds = 3661 let formatter = DateComponentsFormatter() formatter.allowedUnits = [.hour, .minute, .second] formatter.unitsStyle = .full if let formattedString = formatter.string(from: TimeInterval(durationInSeconds)) { print("Duration: \(formattedString)") }

## Output

Duration: 1 hour, 1 minute, 1 second

This code uses the DateComponentsFormatter class to format the duration as a string with hours, minutes, and seconds. The allowedUnits property specifies which units should be included in the output, and the unitsStyle property determines how the units should be formatted. In this case, we are using the .full style, which produces output like "1 hour, 1 minute, 1 second".

The string(from:) method of the DateComponentsFormatter class is used to generate the formatted string. We pass in the duration as a TimeInterval, which is a type alias for Double representing the duration in seconds. If the formatted string can be generated successfully, we print it to the console.

This method can be helpful if you want to arrange the time in a particular manner or tailor the output to the user's preferred region or language. If you are only interested in calculating specific hours, minutes, and seconds numbers, it might be less effective than the prior method.

## Example 4: Converting Using the Quotient and Remainder of the Duration Division

You can use the divmod function to calculate the quotient and remainder of the division of the duration in seconds by 3600 (the number of seconds in an hour) and 60 (the number of seconds in a minute), respectively.

Here is an example of implementation −

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)")

## Output

Duration: 01:01:01

The formatDuration function accepts a durationInSeconds integer as input and outputs structured text with the following notation: "hh:mm:ss," where hh stands for the number of hours, mm for the number of minutes, and ss for the number of seconds.

When the numerator and denominator of two numbers are divided, the divmod function produces a tuple that includes the quotient and residual. The formatDuration function uses this function to determine hours, minutes, and seconds.

## Conclusion

In conclusion, there are several approaches to converting an integer representing a duration in seconds to hours, minutes, and seconds in Swift.

One approach is to use arithmetic operations to calculate the number of hours, minutes, and seconds, and then format them into a string.

Another approach is to use the DateComponentsFormatter class to format the duration as a string with hours, minutes, and seconds.

A third approach is to use the divmod function to calculate the quotient and remainder of the division of the duration in seconds by 3600 and 60, respectively.

Overall, the most appropriate approach depends on your application's specific requirements. If you only need to calculate individual hours, minutes, and seconds values, the arithmetic or divmod approach may be most appropriate. If you need to format the duration as a string, the DateComponentsFormatter class may be more appropriate.