Swift
Integer Conversion
Time Conversion
Programming
Code Tutorial

Swift - Integer conversion to Hours/Minutes/Seconds

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Converting an integer number of seconds into hours, minutes, and seconds is a common Swift task in timers, media players, workout apps, and countdown screens. The simplest and usually best approach is plain integer arithmetic, because the problem is duration formatting, not calendar math.

The Basic Arithmetic

If the input is total seconds, the breakdown is straightforward:

  • hours are total seconds divided by 3600
  • minutes are the remaining seconds divided by 60
  • seconds are the final remainder
swift
1func splitTime(_ totalSeconds: Int) -> (hours: Int, minutes: Int, seconds: Int) {
2    let hours = totalSeconds / 3600
3    let minutes = (totalSeconds % 3600) / 60
4    let seconds = totalSeconds % 60
5    return (hours, minutes, seconds)
6}
7
8let result = splitTime(3661)
9print(result.hours, result.minutes, result.seconds)

For 3661, the result is 1 hour, 1 minute, and 1 second.

This is the right mental model because the calculation is about a duration measured in seconds, not a wall-clock date.

Formatting as HH:MM:SS

Often the real goal is not just the tuple but a display string. Swift string formatting makes that easy.

swift
1func formatHMS(_ totalSeconds: Int) -> String {
2    let hours = totalSeconds / 3600
3    let minutes = (totalSeconds % 3600) / 60
4    let seconds = totalSeconds % 60
5
6    return String(format: "%02d:%02d:%02d", hours, minutes, seconds)
7}
8
9print(formatHMS(5))
10print(formatHMS(65))
11print(formatHMS(3661))

That produces:

  • '00:00:05'
  • '00:01:05'
  • '01:01:01'

This is usually what you want for timer labels or media playback displays.

Handling Long Durations

If the duration can exceed 24 hours, arithmetic still works correctly.

swift
print(formatHMS(90061))

This will show 25:01:01, which is often exactly right for a duration. That is another reason to avoid date-based APIs for this problem. If you use calendar logic, you can accidentally switch from "duration" semantics to "clock time" semantics and get confusing results.

Negative Values Need a Policy

If the input can be negative, decide how you want to display it. A common approach is to preserve the sign and format the absolute value.

swift
1func formatSignedHMS(_ totalSeconds: Int) -> String {
2    let sign = totalSeconds < 0 ? "-" : ""
3    let value = abs(totalSeconds)
4
5    let hours = value / 3600
6    let minutes = (value % 3600) / 60
7    let seconds = value % 60
8
9    return sign + String(format: "%02d:%02d:%02d", hours, minutes, seconds)
10}
11
12print(formatSignedHMS(-3661))

Without an explicit policy, negative inputs can produce surprising component values because integer division and remainder rules become harder to read at a glance.

Returning a Reusable Type

If this logic is used in several places, a small helper type can keep the code clean.

swift
1struct TimeParts {
2    let hours: Int
3    let minutes: Int
4    let seconds: Int
5
6    init(totalSeconds: Int) {
7        self.hours = totalSeconds / 3600
8        self.minutes = (totalSeconds % 3600) / 60
9        self.seconds = totalSeconds % 60
10    }
11
12    var display: String {
13        String(format: "%02d:%02d:%02d", hours, minutes, seconds)
14    }
15}
16
17let time = TimeParts(totalSeconds: 7325)
18print(time.display)

That can make view-model or formatting code easier to read than repeating the arithmetic everywhere.

Why DateComponents Is Usually Overkill

You can involve Date, Calendar, and DateComponents, but for plain duration conversion that is often unnecessary complexity. Those APIs are designed for calendar-aware problems such as dates, time zones, and daylight-saving transitions.

A simple duration such as 3661 seconds does not need a calendar. Integer arithmetic is more direct and less error-prone.

If you do need a duration formatter for user-facing presentation, Foundation also provides specialized formatting tools, but the underlying component split is still usually easiest to express with arithmetic.

Common Pitfalls

One common mistake is using date APIs for a duration problem and accidentally introducing calendar semantics where they are not needed.

Another pitfall is forgetting that % should be applied to the remainder after hours are removed, not to the original value when computing minutes.

A third issue is assuming hours should wrap at 24. For a duration, 25:00:00 is often correct. Wrapping only makes sense if you are formatting a clock time, not elapsed seconds.

Finally, if negative durations are possible, define how the sign should be handled instead of letting integer math produce confusing output.

Summary

  • Convert total seconds to hours, minutes, and seconds with integer division and remainder.
  • Use hours = total / 3600, minutes = (total % 3600) / 60, and seconds = total % 60.
  • Format display strings with zero padding when building timer-style output.
  • Treat long durations as durations, not clock times, so values above 24 hours remain valid.
  • Prefer simple arithmetic over calendar APIs for this problem.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.