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.
Introduction
Converting a number of seconds into hours, minutes, and seconds is a common formatting task in Swift. The logic is simple integer arithmetic, but a good implementation also handles zero padding, negative values, and the distinction between raw components and display strings.
Break Seconds Into Components
The standard approach uses division and remainder operations.
For 3671, the result is 1 hour, 1 minute, and 11 seconds.
Format the Result for Display
Returning the numeric components is useful for calculations, but UI code usually needs a string.
That prints 01:01:11, which is often the most readable representation for timers and media durations.
Handle Durations Under One Hour
Sometimes you want mm:ss when the duration is shorter than an hour.
That gives a shorter format for small values while still expanding naturally for longer ones.
Watch Out for Negative Values
If your app works with countdowns or offsets, negative input needs deliberate handling. Swift’s remainder behavior is mathematically valid, but it may not match a user-friendly display format.
That produces -00:01:15, which is usually clearer than letting negative remainders appear in each component.
Use DateComponentsFormatter When You Need Locale-Aware Output
If you want a higher-level formatter, Foundation can format durations for you.
This is useful when you want consistent system formatting without manually building every string. For straightforward timer displays, manual arithmetic is still easy to read and test.
Keep Calculation Logic Separate from Presentation
A good pattern is to return components from one function and format them in another. That keeps business logic reusable.
This is easier to test than a single function that mixes arithmetic, sign handling, and UI formatting.
Common Pitfalls
A common mistake is computing minutes with totalSeconds / 60, which ignores the hours already removed and produces values larger than 59. Another is forgetting zero padding, which makes timer strings jump in width as values change. Developers also sometimes format negative durations without taking the absolute value first, leading to awkward component output. Finally, if you only need a display string, avoid overcomplicating the problem with full date APIs.
Summary
- Convert seconds with integer division and remainder.
- Use
String(format:)for fixed-width timer output. - Handle negative values explicitly if countdown-style display matters.
- Consider
DateComponentsFormatterfor higher-level formatting. - Separate numeric conversion from presentation when the code will be reused.
Related reading
- Swift - Sort array of objects with multiple criteria
- Swift - specialized _VariantDictionaryBuffer.ensureUniqueNativeBufferInt
- Swift - UIButton with two lines of text
- Swift - UIButton with two lines of text
- Swift - which types to use? NSString or String
- Swift - which types to use? NSString or String
- Swift 2.0 - Binary Operator cannot be applied to two UIUserNotificationType operands
- Swift 2.0 - Binary Operator cannot be applied to two UIUserNotificationType operands
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.