- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Using the toISOString() Method
Users can follow the below syntax for the toISOString() method.
var calculatedTime = new Date( null ); calculatedTime.setSeconds( 5003 ); var newTime = calculatedTime.toISOString().substr( 11, 8 );
<html> <body> <h2>Convert seconds to HH-MM-SS</h2> <h4>Using <i>toISOString()</i> method to convert 5003 seconds to HHMM-SS format.</h4> <div id = "container"></div> </body> <script> var calculatedTime = new Date(null); calculatedTime.setSeconds( 5003 ); //setting value in seconds var newTime = calculatedTime.toISOString().substr(11, 8); document.getElementById("container").innerHTML = newTime; </script> </html>
Using only Math.floor() and Some Mathematical Logic
var hoursLeft = Math.floor( seconds / 3600 ); var min = Math.floor(( seconds - hoursLeft * 3600 ) / 60 ); var secondsLeft = seconds - hoursLeft * 3600 - min * 60; secondsLeft = Math.round( secondsLeft * 100 ) / 100;
Step 1 − Since one hour is made up of 3600 seconds, hours can be computed by multiplying the seconds by 3600. This will reveal how many hours are present.
Step 2 − Use the Math.floor() method to round down the value from the previous step
Step 3 − Once the number of hours has been subtracted, discover the number of remaining seconds. The number of minutes is determined by dividing this amount by 60.
Step 4 − One can determine the seconds by deducting the number of seconds in minutes and hours from the total number previously provided.
With the help of the example given below the user will be able to easily convert seconds into the HH-MM-SS format with the help of some simple mathematical logic.
<html> <body> <h2>Convert seconds to HH-MM-SS</h2> <h4>Using <i>Math.floor()</i> method.</h4> <div id = "container"></div> </body> <script> var seconds = 129; var hoursLeft = Math.floor(seconds / 3600); var min = Math.floor((seconds - hoursLeft * 3600) / 60); var secondsLeft = seconds - hoursLeft * 3600 - min * 60; secondsLeft = Math.round(secondsLeft * 100) / 100; var answer = hoursLeft< 10 ? "0" + hoursLeft : hoursLeft; answer += ":" + (min < 10 ? "0" + min : min); answer += ":" + (secondsLeft< 10 ? "0" + secondsLeft : secondsLeft); document.getElementById("container").innerHTML = "129 seconds to time is : " + answer; </script> </html>
Libraries like day.js, Luxon, and Date-fns are some alternatives to moment.js that can be used to directly get time in a specific format. All the methods work just fine but should be used according to the user's needs.
- Display seconds with SimpleDateFormat('ss') in Java
- How to convert MM/YY to YYYY-MM-DD in MYSQL?
- How to convert MM/YY to YYYY-MM-DD with a specific day in MySQL?
- Display Seconds in ss format (01, 02) in Java
- MySQL query to convert timediff() to seconds?
- Python program to convert seconds into hours, minutes and seconds
- How to convert Python date string mm/dd/yyyy to datetime?
- Convert MySQL date format from yyyy-mm-ddThh:mm:ss.sssZ to yyyy-mm-dd hh:mm:ss ?
- MySQL - Convert YYYY-MM-DD to UNIX timestamp
- How to convert time seconds to h:m:s format in Python?
- How to convert date YYYYMMDD to YY-MM-DD in MySQL query?
- MySQL date format to convert dd.mm.yy to YYYY-MM-DD?