if you want to convert a given time duration into a more human-readable format, including hours, minutes, seconds and miliseconds. In this tutorial, we will show you how to convert hours to minutes, minutes to seconds and seconds to millisecond, convert minutes to seconds and milliseconds, how to convert seconds to milliseconds, convert date string to milliseconds in javascript
How to Convert Hours to Minutes,Minutes to Seconds,date to Milliseconds in JavaScript
Here are a few approaches to achieve this task. Here are a few examples:
- Approach 1 – Convert Hours to Minutes
- Approach 2 – Convert Hours to Seconds
- Approach 3 – Convert Hours to MilliSeconds
- Approach 4 – Convert Minutes to Seconds
- Approach 5 – Convert Minutes to MilliSeconds
- Approach 6 – Convert Seconds to MilliSeconds
- Approach 7 – Convert Date to MilliSeconds
- Approach 8 – Convert Seconds to hh mm ss
Approach 1 – Convert Hours to Minutes
Converting hours to minutes in JavaScript is a simple task. You can achieve this by multiplying the number of hours by 60 since there are 60 minutes in an hour. Here is a JavaScript function to convert hours to minutes:
function convertHourstoMinute(hours) { return Math.floor(hours * 60); }
Here’s an example to convert hours to minutes:
<!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
Approach 2 – Convert Hours to Seconds
To convert hours to seconds in JavaScript. You need to multiply the number of hours by 3600, considering that there are 60 seconds in a minute and 60 minutes in an hour. Here is a JavaScript function to convert hours to seconds:
function convertHourstoSeconds(hours) { return Math.floor(hours * 60 * 60); }
Here’s an example to convert hours to seconds:
<!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
Approach 3 – Convert Hours to MilliSeconds
Converting hours to milliseconds in JavaScript is a easy task. Since there are 60 seconds in a minute, and 60 minutes in an hour, you can achieve this conversion by multiplying the number of hours by 3,600,000 (60 seconds * 60 minutes * 1000 milliseconds). Here’s a simple JavaScript function for this conversion:
function convertHourstoMilliSecond(hours) { return Math.floor(hours * 60 * 60 *1000); }
Here’s an example to convert hours to milliseconds:
<!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
Approach 4 – Convert Minutes to Seconds
You multiply the number of minutes by 60, you can get the equivalent in seconds, because there are 60 seconds in each minute. Here is the JavaScript function to convert minutes to seconds:
function convertMinutestoSeconds(minutes) { return Math.floor(minutes * 60); }
Here is an example to convert minutes to seconds:
<!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
Approach 5 – Convert Minutes to MilliSeconds
Converting minutes to milliseconds in JavaScript involves multiplying the number of minutes by 60,000, as there are 60 seconds in a minute and 1,000 milliseconds in a second. Here’s a simple JavaScript function for this conversion:
function convertMinutestoMilliSeconds(minutes) { return Math.floor(minutes * 60 * 1000); }
Here’s an example to convert minutes to milliseconds:
<!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
Approach 6 – Convert Seconds to MilliSeconds
To convert seconds to milliseconds in JavaScript is a very easy. Since there are 1000 milliseconds in a second, you can achieve this by multiplying the number of seconds by 1000. Here’s a JavaScript function to perform this conversion:
function convertSecondstoMilliSeconds(seconds) { return Math.floor(seconds * 1000); }
Here’s example to convert seconds to milliseconds:
<!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
Approach 7 – Convert Date to MilliSeconds
In JavaScript, you can convert a date to milliseconds using the getTime()
method of the Date
object. The getTime()
method returns the numeric value corresponding to the time for the specified date according to universal time.
var date = new Date(); var milliseconds = date.getTime();
Here’s an example to convert date to milliseconds:
<!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:-
Approach 8 – Convert Seconds to hh mm ss
To convert seconds to the “hh:mm:ss” format in JavaScript, you can use the following function:
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; }
Here’s an example to convert seconds to the “hh:mm:ss” format:
<!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
You may like
- Compare two dates with JavaScript – Examples
- JavaScript Get Month Name With Various Examples
- Get Month 2 Digits in JavaScript
- javaScript Get Current Year 2 and 4 Digit – Example
- Get Current Date Time javaScript
- JavaScript: Date setUTCDate() Method
- Set UTC Month In javaScript
- setUTCFullYear() Method JavaScript With Examples
- javascript date setUTCHours() Method With Examples
- JavaScript: d.setUTCMinutes() Method e.g.
- setUTCSecond() Method By JavaScript
- javaScript Date set UTC milliseconds
- setUTCSecond() Method By JavaScript
- JavaScript: Date.getUTCDate() Method
- getUTCmonth Method javaScript With Example
- Digital Clock with date in javaScript
- JavaScript: Set Date Methods
- JavaScript Get Date Methods
If you have any questions or thoughts to share, use the comment form below to reach us.