javascript Convert Hours to Minutes,Minutes to Seconds,date to Milliseconds

javascript Convert Hours to Minutes,Minutes to Seconds,date to Milliseconds

In this javascript convert time tutorial. we will explain, In JavaScript how to convert hours to minutes, seconds and millisecond, how to convert minutes to seconds and milliseconds, how to convert seconds to milliseconds, how to convert date string to milliseconds in javascript

Content – JavaScript Convert Time

In this tutorial, we will take the following examples:-

  • First Function – javascript convert hours to minutes
  • Second Function – convert hours to seconds in javascript
  • Third Function – convert hours to milliseconds javascript
  • Fourth Function – convert minutes to seconds in javascript
  • Five Function – convert minutes to Milliseconds in javascript
  • Six function – convert seconds to milliseconds javascript
  • Seven Function – convert date to milliseconds javascript
  • Eight Function – javascript convert seconds to hh mm ss

First Function – javascript convert hours to minutes

Here we will create a function, which is used to convert hours to minutes in javascript. The function is below:

    function convertHourstoMinute(hours) {
     return Math.floor(hours * 60);
    }

Ex:-

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>javascript convert hours to minutes</title>
</head>
<body>
  <script type = "text/javascript">
    function convertHourstoMinute(hours) {
     return Math.floor(hours * 60);
    }
    var minutes = convertHourstoMinute(2); // convert hours into minutes javascript
    document.write( "javascript convert hours to minutes :- " + minutes ); 

  </script>  
</body>
</html>

Result of the above code is: javascript convert hours to minutes :- 120 Minutes

Second Function – convert hours to seconds in javascript

Next, we will create a function, which is used to convert hours to seconds in javascript. In this function, we will pass hours and it returns the seconds. The function is below

    function convertHourstoSeconds(hours) 
    {
      return Math.floor(hours * 60 * 60);
    }

Ex:-

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>javascript convert hours to seconds<</title>
</head>
<body>
  <script type = "text/javascript">
    function convertHourstoSeconds(hours) 
    {
      return Math.floor(hours * 60 * 60);
    }
    var hoursToSeconds = convertHourstoSeconds(2); // convert hours into second javascript 
    document.write( "Result of converting hours to seconds :- " + hoursToSeconds ); 

  </script>  
</body>
</html>

Above code result of converting hours to seconds:- 7200 Seconds

Third Function – convert hours to milliseconds javascript

Here, we will create a function, which is used to convert hours to milliseconds in javascript. In this function, we will pass hours and it returns the value in milliseconds. The function is below

    function convertHourstoMilliSecond(hours) 
    {
      return Math.floor(hours * 60 * 60 *1000);
    }

Ex:-

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>convert hours to milliseconds javascript</title>
</head>
<body>
  <script type = "text/javascript">
    function convertHourstoMilliSecond(hours) 
    {
      return Math.floor(hours * 60 * 60 *1000);
    }
    var hoursToMilliSecond = convertHourstoMilliSecond(2); // convert hours into milli second javascript 
    document.write( "Result of converting hours to milliseconds :- " + hoursToMilliSecond ); 

  </script>  
</body>
</html>

Above code result of converting hours to milliseconds:- 7200000 Milliseconds

Fourth Function – convert minutes to seconds in javascript

Here, we will create a function, which is used to convert minutes to seconds in javascript. In this function, we will pass minutes and it returns the value in seconds. The function is below

    function convertMinutestoSeconds(minutes) 
    {
      return Math.floor(minutes * 60);
    }

Ex:-

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>convert minutes to seconds in javascript</title>
</head>
<body>
  <script type = "text/javascript">
    function convertMinutestoSeconds(minutes) 
    {
      return Math.floor(minutes * 60);
    }
    var minutesToSeconds = convertMinutestoSeconds(2); // convert minutes to second javascript 
    document.write( "Result of converting minutes to seconds :- " + minutesToSeconds ); 

  </script>  
</body>
</html>

Result of converting minutes to seconds:- 120 Seconds

Five Function – convert minutes to Milliseconds in javascript

Here, we will create a function, which is used to convert minutes to milliseconds in javascript. In this function, we will pass minutes and it returns the value in milliseconds. The function is below

    function convertMinutestoMilliSeconds(minutes) 
    {
      return Math.floor(minutes * 60 * 1000);
    }

Ex:-

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>convert minutes to Milli seconds in javascript</title>
</head>
<body>
  <script type = "text/javascript">
    function convertMinutestoMilliSeconds(minutes) 
    {
      return Math.floor(minutes * 60 * 1000);
    }
    var minutesToMilliSeconds = convertMinutestoMilliSeconds(2); // convert minutes to second javascript 
    document.write( "Result of converting minutes to milli seconds :- " + minutesToMilliSeconds ); 

  </script>  
</body>
</html>

Result of converting minutes to seconds:- 120000 MilliSeconds

Six function – convert seconds to milliseconds javascript

Here, we will create a function, which is used to convert seconds to milliseconds in javascript. In this function, we will pass seconds and it returns the value in milliseconds. The function is below

    function convertSecondstoMilliSeconds(seconds) 
    {
      return Math.floor(seconds * 1000);
    }
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>convert seconds to Milli seconds in javascript</title>
</head>
<body>
  <script type = "text/javascript">
    function convertSecondstoMilliSeconds(seconds) 
    {
      return Math.floor(seconds * 1000);
    }
    var secondsToMilliSeconds = convertSecondstoMilliSeconds(2); // convert minutes to second javascript 
    document.write( "Result of converting seconds to milli seconds :- " + secondsToMilliSeconds ); 

  </script>  
</body>
</html>

Above code result of converting seconds to milliseconds:- 2000 ms

Seven Function – convert date to milliseconds javascript

Now we will take an example for convert date string to milliseconds javascript using the javascript getTime() method.

    var date = new Date(); 
    var milliseconds = date.getTime(); 

Ex-

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>convert date to Milliseconds in javascript</title>
</head>
<body>
  <script type = "text/javascript">
    var date = new Date(); 
    var milliseconds = date.getTime(); 
    document.write( "Result of converting date to milliseconds :- " + milliseconds );
  </script>  
</body>
</html>

Result of converting date to milliseconds is below:-

convert date to Milliseconds in javascript

Eight Function – javascript convert seconds to hh mm ss

Here, we will create a function, which is used to convert seconds to hh mm ss format in javascript. In this function, we will pass seconds and it returns the value in hh mm ss format. The function is below

      function convertSecondsTo(sec) 
      {
          var hours = Math.floor(sec / 3600);
          var minutes = Math.floor((sec - (hours * 3600)) / 60);
          var seconds = sec - (hours * 3600) - (minutes * 60);
          seconds = Math.round(seconds * 100) / 100
         
          var result = (hours < 10 ? "0" + hours : hours);
          result += "-" + (minutes < 10 ? "0" + minutes : minutes);
          result += "-" + (seconds < 10 ? "0" + seconds : seconds);
          return result;
     }

Ex:-

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>javascript convert seconds to hh mm ss</title>
</head>
<body>
  <script type = "text/javascript">
      function convertSecondsTo(sec) 
      {
          var hours = Math.floor(sec / 3600);
          var minutes = Math.floor((sec - (hours * 3600)) / 60);
          var seconds = sec - (hours * 3600) - (minutes * 60);
          seconds = Math.round(seconds * 100) / 100
         
          var result = (hours < 10 ? "0" + hours : hours);
          result += "-" + (minutes < 10 ? "0" + minutes : minutes);
          result += "-" + (seconds < 10 ? "0" + seconds : seconds);
          return result;
     }

    var secondsTo = convertSecondsTo(1000); // convert seconds in HH-MM-SS format javascript 
    document.write( "Result of converting to seconds in HH-MM-SS format :- " + secondsTo ); 

  </script>  
</body>
</html>

Above code Result of converting to seconds in HH-MM-SS format:- 00-16-40

If you want to know more about javascript date and time methods, you may like following date and time methods:

You may like

  1. Compare two dates with JavaScript – Examples
  2. JavaScript Get Month Name With Various Examples
  3. Get Month 2 Digits in JavaScript
  4. javaScript Get Current Year 2 and 4 Digit – Example
  5. Get Current Date Time javaScript
  6. JavaScript: Date setUTCDate() Method
  7. Set UTC Month In javaScript
  8. setUTCFullYear() Method JavaScript With Examples
  9. javascript date setUTCHours() Method With Examples
  10. JavaScript: d.setUTCMinutes() Method e.g.
  11. setUTCSecond() Method By JavaScript
  12. javaScript Date set UTC milliseconds
  13. setUTCSecond() Method By JavaScript
  14. JavaScript: Date.getUTCDate() Method
  15. getUTCmonth Method javaScript With Example
  16. Digital Clock with date in javaScript
  17. JavaScript: Set Date Methods
  18. JavaScript Get Date Methods

If you have any questions or thoughts to share, use the comment form below to reach us.

Leave a Reply

Your email address will not be published. Required fields are marked *