# Coding Challenge - #6: Format Elapsed Time

**URL:** <https://forum.kirupa.com/t/coding-challenge-6-format-elapsed-time/683064>\
**Category:** web dev\
**Created:** [August 15, 2026, 10:00pm UTC](https://forum.kirupa.com/t/coding-challenge-6-format-elapsed-time/683064 "2026-08-15T22:00:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sarah\_connor](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sarah_connor/32/31258_2.png) [@sarah\_connor](https://forum.kirupa.com/u/sarah_connor)\
**Post date:** [August 15, 2026, 10:00pm UTC](https://forum.kirupa.com/t/coding-challenge-6-format-elapsed-time/683064/1 "2026-08-15T22:00:06Z")

</div>

Write a function `formatDuration(seconds)` that converts a non-negative integer of seconds into a `"MM:SS"` or `"HH:MM:SS"` string. Omit the hours portion if the duration is under one hour, and ensure all parts are zero-padded to two digits (for example, `65` becomes `"01:05"` and `3661` becomes `"01:01:01"`).

```js
function formatDuration(seconds) {
  // Your code here
}

```

**Rules:**

- Assume seconds is always an integer \>= 0
- Single-digit values must be zero-padded (e.g. 5 seconds → “00:05”)
- Return “HH:MM:SS” only when seconds \>= 3600, otherwise return “MM:SS”

Post your solution as a reply. Answer goes up in about a day.

---

<div class="post-metadata">

**Author:** ![Quelly](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/quelly/32/31386_2.png) [@Quelly](https://forum.kirupa.com/u/Quelly)\
**Post date:** [August 16, 2026, 10:00pm UTC](https://forum.kirupa.com/t/coding-challenge-6-format-elapsed-time/683064/2 "2026-08-16T22:00:20Z")

</div>

`padStart` is definitely the move here, saves so much boilerplate. I always forget about it until I’m halfway through writing an `if (num < 10)` check.

```auto
function formatDuration(seconds) {
  const pad = (num) => String(num).padStart(2, '0');

  const hours = Math.floor(seconds / 3600);
  const minutes = Math.floor((seconds % 3600) / 60);
  const remainingSeconds = seconds % 60;

  if (seconds >= 3600) {
    return `${pad(hours)}:${pad(minutes)}:${pad(remainingSeconds)}`;
  } else {
    return `${pad(minutes)}:${pad(remainingSeconds)}`;
  }
}

```

---

<div class="post-metadata">

**Author:** ![sarah\_connor](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sarah_connor/32/31258_2.png) [@sarah\_connor](https://forum.kirupa.com/u/sarah_connor)\
**Post date:** [August 16, 2026, 11:00pm UTC](https://forum.kirupa.com/t/coding-challenge-6-format-elapsed-time/683064/3 "2026-08-16T23:00:11Z")

</div>

**Challenge solution:** The solution converts total seconds into hours, minutes, and seconds, then formats these components with zero-padding based on the total duration.

**One way to do it:**

```js
function formatDuration(seconds) {
  const pad = (num) => num.toString().padStart(2, '0');

  const hours = Math.floor(seconds / 3600);
  const minutes = Math.floor((seconds % 3600) / 60);
  const remainingSeconds = seconds % 60;

  if (seconds >= 3600) {
    return `${pad(hours)}:${pad(minutes)}:${pad(remainingSeconds)}`;
  } else {
    return `${pad(minutes)}:${pad(remainingSeconds)}`;
  }
}

```

**Why:**  
This function first defines a helper `pad` function to ensure two-digit zero-padding for any number. It then calculates hours, minutes, and remaining seconds using integer division and modulo operations. Finally, it conditionally formats the output string to ‘HH:MM:SS’ if the total seconds are 3600 or more, otherwise it returns ‘MM:SS’, ensuring all parts are correctly padded.

**First-answer leaderboard**

1. @kirupa - 4 (firsts) 🏆
2. @adnanahmed - 2 (firsts)
3. @emmawalter5 - 1 (first)
