. Advertisement .
..3..
. Advertisement .
..4..
Are you working on a project that involves time in JavaScript? Making things for users to understand the time? When you have time in Seconds, it will be difficult for the users to understand it as the standard time that users understand is in hours, minutes, and seconds. So, for this, the first thing you need to make the time user-friendly, you need to convert the seconds to HH:MM:SS in JavaScript.
So, the question arises of how to get the time in HH:MM:SS. Check out what you need to do
How To Convert Seconds to HH:MM:SS in JavaScript
Converting seconds to HH:MM:SS is really a simple process. Four procedures are used to convert it into the standard hour, minute, and second format. Check out what you need to do
- To get milliseconds, multiply the seconds by ‘1000’.
- Use
Date()
Constructor for passing milliseconds. - The
toISOString()
method on the ‘Date’. - Get the hh:mm:ss
Code:
const sample_seconds = 1000;
// Get hh:mm:ss string
const result1 = new Date(sample_seconds * 1000).toISOString().slice(11, 19);
console.log(result1);
Output:
00:16:40
If seconds are less than 1 hour and you only need mm:ss, you need to run the following code:
const sample_seconds2 = 1100;
const result2 = new Date(sample_seconds2 * 1000).toISOString().slice(14, 19);
console.log(result2);
Output:
18:20
Here, multiplying seconds by ‘1000’ returns milliseconds, then the milliseconds took by the Date()
constructor and return the Date object that we use control inbuilt methods.
The toISOString() Method
To get the date in ISO format ‘YYYY-MM-DDTHH:mm:ss:sssZ, the toISOString()
method is used. Have look at the code:
const sample_seconds3 = 1250;
console.log(new Date(sample_seconds3 * 1000).toISOString());
Output:
1970-01-01T00:20:50.000Z
Here, in this code, you can see that there is a ‘T’ character. The purpose of the ‘T’ character is the marking point that separates it from the time. Whereas, you can also see a ‘dot’, which is placed to separate the milliseconds.
The slice() Method
It Is used to obtain the substring from the character at the ‘11’ index up to the ‘19’ index. This example is to explain the method that we used in the first code where we used index ’11’ and ‘19’.
Have a look at the example:
const sample_seconds4 = 1300;
console.log(new Date(sample_seconds4 * 1000).toISOString());
const result3 = new Date(sample_seconds4 * 1000).toISOString().slice(11, 19);
console.log(result3);
Output:
1970-01-01T00:21:40.000Z
00:21:40
Use ‘mm:ss’ Format
There is a scenario. When you are converting seconds, and then you find out that the seconds that you are trying to convert are less than ‘1’ hour. We need to still get it converted, and this is how you can do it:
const sample_seconds5 = 1400;
console.log(new Date(sample_seconds5 * 1000).toISOString());
const result4 = new Date(sample_seconds5 * 1000).toISOString().slice(14, 19);
console.log(result4);
Output:
1970-01-01T00:23:20.000Z
23:20
Here, again slice()
method is used to get the substring from the ‘14’ character to the ‘19’ character. We get ‘mm:ss’ format in return.
Conclusion
The easiest methods and operators are used to convert seconds to HH:MM:SS in JavaScript. I hope you code well when trying to convert the time format.
Leave a comment