JavaScript can convert a duration given in seconds into an hh:mm:ss formatted string by separating the total seconds into hours, minutes, and seconds.
- Convert the given seconds into hours, minutes, and seconds.
- Format each value to two digits using padStart().
- Combine the values using : to produce the hh:mm:ss format.
Approach 1: Using a Date Object
In this approach, the given seconds are converted into milliseconds and passed to a Date object. The getUTCHours(), getUTCMinutes(), and getUTCSeconds() methods are then used to extract the time components.
dateObj = new Date(given_seconds * 1000);
hours = dateObj.getUTCHours();
minutes = dateObj.getUTCMinutes();
seconds = dateObj.getUTCSeconds();
timeString = hours.toString().padStart(2, '0') + ':' +
minutes.toString().padStart(2, '0') + ':' +
seconds.toString().padStart(2, '0');
Example: Converts 3685 seconds into the hh:mm:ss format using a Date object.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Convert Seconds to Time String</title>
</head>
<body>
<h1 style="color: green">
GeeksforGeeks
</h1>
<b>
JavaScript seconds to time
string with format hh:mm:ss
</b>
<p>No. of seconds is: 3685</p>
<p>
Time in hh:mm:ss is:
<span class="output"></span>
</p>
<button onclick="convertSecondstoTime()">
Get time in hh:mm:ss
</button>
<script>
function convertSecondstoTime() {
let given_seconds = 3685;
let dateObj = new Date(given_seconds * 1000);
let hours = dateObj.getUTCHours();
let minutes = dateObj.getUTCMinutes();
let seconds = dateObj.getUTCSeconds();
let timeString =
hours.toString().padStart(2, '0') + ':' +
minutes.toString().padStart(2, '0') + ':' +
seconds.toString().padStart(2, '0');
document.querySelector('.output').textContent =
timeString;
}
</script>
</body>
</html>
Approach 2: Calculating Hours, Minutes, and Seconds Individually
In this approach, the total number of seconds is divided to calculate the hours, minutes, and remaining seconds. This method does not require creating a Date object and works directly with the duration.
Syntax:
hours = Math.floor(given_seconds / 3600);
minutes = Math.floor((given_seconds - (hours * 3600)) / 60);
seconds = given_seconds - (hours * 3600) - (minutes * 60);
timeString = hours.toString().padStart(2, '0') + ':' +
minutes.toString().padStart(2, '0') + ':' +
seconds.toString().padStart(2, '0');
Example: Converts 3685 seconds into the hh:mm:ss format by calculating each time component individually.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Convert Seconds to Time String</title>
</head>
<body>
<h1 style="color: green">
GeeksforGeeks
</h1>
<b>
JavaScript seconds to time
string with format hh:mm:ss
</b>
<p>No. of seconds is: 3685</p>
<p>
Time in hh:mm:ss is:
<span class="output"></span>
</p>
<button onclick="convertSecondstoTime()">
Get time in hh:mm:ss
</button>
<script>
function convertSecondstoTime() {
let given_seconds = 3685;
let hours = Math.floor(given_seconds / 3600);
let minutes = Math.floor(
(given_seconds - (hours * 3600)) / 60
);
let seconds =
given_seconds - (hours * 3600) -
(minutes * 60);
let timeString =
hours.toString().padStart(2, '0') + ':' +
minutes.toString().padStart(2, '0') + ':' +
seconds.toString().padStart(2, '0');
document.querySelector('.output').textContent =
timeString;
}
</script>
</body>
</html>