Convert Milliseconds to Seconds, Minutes, and Hours in JavaScript
JavaScript can sometimes make working with time a bit difficult. In this post, we’ll learn how to convert milliseconds to seconds, minutes, and hours using vanilla JavaScript.
We’re going to start with a value, in milliseconds, and convert it to seconds, minutes, and hours. The end result should be a string with the number of hours, minutes, and seconds in the HH:MM:SS format.
Convert to Seconds
Let’s say you started with this value:
To get the seconds, we can divide the milliseconds by 1000.
Let’s use Math.floor to round the number down to the nearest whole number.
Now, this is the number of seconds for the entire duration. We only want the number of seconds left over, so we can use the modulus operator.
Convert to Minutes
Using the same process as we did for seconds, we can get the minutes. This time we have to divide the milliseconds by 1000 , then by 60 , then modulus it by 60 .
Again, we want to use Math.floor to round the minutes down to the nearest whole number.
Convert to Hours
Finally, we can convert the milliseconds to hours. This time we have to divide the milliseconds by 1000 , then by 60 , then by 60 again.
However, this time we need to use modulus 24 since there are 24 hours in a day.
Great, now we have the number of hours, minutes, and seconds. We can use the HH:MM:SS format to display the time.
Formatting the Time
With all three numbers calculated, we can move on to formatting the time.
We can take advantage of JavaScript’s type coercion to convert the numbers to strings and then use padStart to add a leading zero to the number if it’s less than 10.
From there, we can join the formatted time together with a colon to get our final result.
Now we can just wrap this up in a nice function and call it to get the formatted time.
< const seconds = Math.floor((milliseconds / 1000) % 60); const minutes = Math.floor((milliseconds / 1000 / 60) % 60); const hours = Math.floor((milliseconds / 1000 / 60 / 60) % 24); return [ hours.toString().padStart(2, "0"), minutes.toString().padStart(2, "0"), seconds.toString().padStart(2, "0") ].join(":"); >const formattedTime = formatTime(milliseconds); console.log(formattedTime); // 21:12:09
Conclusion
JavaScript can make it difficult to work with time. However, we learned how to convert milliseconds to seconds, minutes, and hours using vanilla JavaScript and even formatted the time to be in the HH:MM:SS format.
Now you can use that function in any of your JavaScript projects to convert milliseconds to hours, minutes, and seconds!
If you want to learn about web development, founding a start-up, bootstrapping a SaaS, and more, follow me on Twitter! You can also join the conversation over at our official Discord!
Give feedback on this page , tweet at us, or join our Discord !
Get current date/time in seconds — JavaScript?
Let’s dive into the article for getting better understanding on getting current date time in seconds.
Using Date.now() method
The Date.now() static method returns the milliseconds that have passed since the epoch, which is recognized as the beginning of January 1, 1970, UTC at midnight.
Syntax
Following is the syntax for Date.now() −
Example
In the following example, we are running the script using Date.now() to get the date and time in seconds.
Current date/time in seconds is:
When the script gets executed, it will generate an output consisting of the text along with a click button on the webpage. If the user clicks the button, the event gets triggered and displays the date and time in seconds on the webpage.
Using new Date.getTime() method
JavaScript date getTime() method returns the numeric value corresponding to the time for the specified date according to universal time. The value returned by the getTime method is the number of milliseconds since 1 January 1970 00:00:00.
Syntax
Following is the syntax for new Date.getTime() −
Example
Consider the following example, here we are running the script using Date.getTime() to get the date and time in seconds.
Current date/time in seconds is:
On running the above script, the output window will pop up, displaying the text along with a click button on the webpage. When the user clicks the button, the event gets triggered and gets the time in seconds.
Example
Execute the below code and observe how we are getting the date and time in seconds.
When the script gets executed, the output window will pop up, triggering the event and displaying the current date and time based on IST and also the date and time in seconds on the webpage.