Convert seconds to HH:MM:SS and vice versa in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 6, 2024
3 min

banner

# Table of Contents

  1. Convert seconds to HH:MM:SS in JavaScript
  2. Convert HH:MM:SS to Seconds using JavaScript

# Convert seconds to HH:MM:SS in JavaScript

To convert seconds to HH:MM:SS:

  1. Multiply the seconds by 1000 to get milliseconds.
  2. Pass the milliseconds to the Date() constructor.
  3. Use the toISOString() method on the Date object.
  4. Get the hh:mm:ss portion of the string.
index.js
const seconds = 600; // โœ… get hh:mm:ss string const result = new Date(seconds * 1000) .toISOString() .slice(11, 19); console.log(result); // ๐Ÿ‘‰๏ธ "00:10:00" (hh:mm:ss) // โœ… if seconds are less than 1 hour and you only need mm:ss const result2 = new Date(seconds * 1000) .toISOString() .slice(14, 19); console.log(result2); // ๐Ÿ‘‰๏ธ "10:00" (mm:ss)

convert seconds to hh mm ss

The code for this article is available on GitHub

We converted the seconds to milliseconds by multiplying them by 1000.

The Date() constructor takes milliseconds as a parameter and returns a Date object that we used to leverage built-in methods.

The toISOString() method returns a string representation of the date in the ISO 8601 format - YYYY-MM-DDTHH:mm:ss.sssZ.

index.js
const seconds = 600; // ๐Ÿ‘‡๏ธ "1970-01-01T00:10:00.000Z" console.log(new Date(seconds * 1000).toISOString());
At this point, all we have to do is get the portion of the string between the T character, which is the marker that the time portion of the date starts, and the dot ., which precedes the milliseconds.

We used the String.slice() method to get the substring from the character at index 11 up to (but not including) the character at index 19.

index.js
const seconds = 600; // ๐Ÿ‘‡๏ธ "1970-01-01T00:10:00.000Z" console.log(new Date(seconds * 1000).toISOString()); // โœ… get hh:mm:ss string const result = new Date(seconds * 1000).toISOString().slice(11, 19); console.log(result); // ๐Ÿ‘‰๏ธ "00:10:00" (hh:mm:ss)

You can format the time as mm:ss if the seconds you want to convert are less than 1 hour (3600 seconds).

index.js
const seconds = 600; // ๐Ÿ‘‡๏ธ "1970-01-01T00:10:00.000Z" console.log(new Date(seconds * 1000).toISOString()); // โœ… if seconds are less than 1 hour and you only need mm:ss const result2 = new Date(seconds * 1000).toISOString().slice(14, 19); console.log(result2); // ๐Ÿ‘‰๏ธ "10:00" (mm:ss)

This time we used the slice method to get a substring from the character at index 14 up to (but not including) character at index 19.

This only returns the mm:ss portion of the string.

# Convert HH:MM:SS to Seconds using JavaScript

To convert hh:mm:ss to seconds:

  1. Convert the hours to seconds, by multiplying by 60 twice.
  2. Convert the minutes to seconds by multiplying by 60.
  3. Sum the results for the hours and minutes with the seconds to get the final value.
index.js
const str = '09:30:16'; const [hours, minutes, seconds] = str.split(':'); function convertToSeconds(hours, minutes, seconds) { return ( Number(hours) * 60 * 60 + Number(minutes) * 60 + Number(seconds) ); } console.log(convertToSeconds(hours, minutes, seconds)); // ๐Ÿ‘‰๏ธ 34216

convert hh mm ss to seconds

The code for this article is available on GitHub
We created a reusable function, which takes hours, minutes and seconds as parameters and converts them to seconds.

The string in the example is formatted as hh:mm:ss, but this could be any other format.

In the example, we split the string on each colon, to get an array of substrings. The array contains the hours, minutes and seconds.

index.js
const str = '09:30:16'; // ๐Ÿ‘‡๏ธ ['09', '30', '16'] console.log(str.split(':'))

We then directly assigned the values to the hours, minutes and seconds variables using array destructuring.

To convert the hours to seconds, we had to multiply the value by 60 twice - once to convert to minutes and the second time to convert to seconds.

To convert the minutes to seconds, we multiplied by 60 once.

Because the results of the split operation are strings, we converted each value to a number and added them to one another.

If you only have a string that contains the hours and minutes (hh:mm), you can slightly tweak the function to convert to seconds.

index.js
const str = '09:30'; const [hours, minutes] = str.split(':'); function convertToSeconds(hours, minutes) { return Number(hours) * 60 * 60 + Number(minutes) * 60; } console.log(convertToSeconds(hours, minutes)); // ๐Ÿ‘‰๏ธ 34200
The code for this article is available on GitHub

The string above is formatted as hh:mm, so we removed the third parameter of the function and converted the hours and minutes to seconds.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev