Last updated: Mar 6, 2024
Reading timeยท3 min
To convert seconds to HH:MM:SS
:
1000
to get milliseconds.Date()
constructor.toISOString()
method on the Date
object.hh:mm:ss
portion of the string.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)
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
.
const seconds = 600; // ๐๏ธ "1970-01-01T00:10:00.000Z" console.log(new Date(seconds * 1000).toISOString());
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
.
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).
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.
To convert hh:mm:ss
to seconds:
60
twice.60
.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
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.
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.
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.
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 string above is formatted as hh:mm
, so we removed the third parameter of
the function and converted the hours and minutes to seconds.
You can learn more about the related topics by checking out the following tutorials: