JavaScript program to convert a seconds value to HH:mm:ss:
Let me show you how to convert a second value to HH:mm:ss or hour, minute, seconds format. We can use the toISOString() and the Date constructor to do the conversion.
Date constructor:
If we pass an integer time value to the JavaScript Date constructor, it will consider it as the number of milliseconds since January 1, 1970, 00:00:00 UTC. So, we can multiply the seconds value with 1000 to convert it to milliseconds and pass it to the Date constructor to create an instance of Date.
Other methods of the Date class can be called on this instance.
Date toISOString() method:
The toISOString method is an inbuilt method of the Date class. It returns a string representation of the date in ISO 8601. It looks as below:
YYYY-MM-DDTHH:mm:ss.sssZ
As you can see here, it includes the required HH:mm:ss values.
How to convert a seconds value to HH:mm:ss in JavaScript:
As explained above, we can follow these steps to convert a seconds value to HH:mm:ss:
- Get the time in seconds from the user.
- Multiply it by 1000 to convert it to milliseconds.
- Pass the milliseconds to the Date constructor to create an instance of Date.
- Call the toISOString() method on this instance to get the ISO 8601 format date-time string.
- As the output of toISOString() is in
YYYY-MM-DDTHH:mm:ss.sssZ
format, we have to pick the substring from index 11 to 19. We can use the slice() method.
If I combine these steps, the complete program will be:
let seconds = 2200;
let date = new Date(seconds * 1000);
let isoDate = date.toISOString();
let timeStr = isoDate.slice(11, 19);
console.log("HH:mm:ss : ", timeStr);
- The seconds is assigned to the variable seconds.
- One instance of Date is created by passing the milliseconds value to the Date constructor.
- The ISO string is assigned to the variable isoDate.
- By using the slice method, the HH:mm:ss time string is created and assigned it to the timeStr variable.
- The last line is printing the result time string.
If you run this program, it will print the below output:
HH:mm:ss : 00:36:40
How to print the time in HH-mm-ss format:
The above example converted the time to HH:mm:ss, but we can also change it to any other formats we want. For example, if we want to get the time in HH-mm-ss format, we can replace all : with - in HH:mm:ss. JavaScript string has one method called replaceAll to replace all occurrences of a substring in a string.
let seconds = 4222;
let date = new Date(seconds * 1000);
let isoDate = date.toISOString();
let timeStr = isoDate.slice(11, 19).replaceAll(':', '-');
console.log("HH-mm-ss : ", timeStr);
It will print:
HH-mm-ss : 01-10-22
You might also like:
- How to remove object properties in JavaScript
- How to convert objects to string in JavaScript
- How to get all Keys of a JavaScript Object
- JavaScript program to print the ASCII values from A to Z alphabet
- 4 different JavaScript program to find the factorial of a number
- 3 ways in JavaScript to remove duplicate items from an array
- How to sort the array elements in descending order in JavaScript