Convert hours to minutes, minutes to seconds, date to milliseconds in JavaScript; In this 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 Show
Table of Contents
Content – JavaScript Convert TimeIn this tutorial, we will take the following examples:-
First Function – javascript convert hours to minutesHere 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 javascriptNext, 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 javascriptHere, 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 javascriptHere, 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 javascriptHere, 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 javascriptHere, 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 javascriptNow 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 javascriptEight Function – javascript convert seconds to hh mm ssHere, 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
If you have any questions or thoughts to share, use the comment form below to reach us. How do you convert hours minutes seconds to milliseconds?To convert milliseconds to hours, minutes, seconds: Divide the milliseconds by 1000 to get the seconds. Divide the seconds by 60 to get the minutes. Divide the minutes by 60 to get the hours. How do you convert hours and minutes to milliseconds in Java?1 minute = 60 * seconds, 1 second = 1000 millis => 1 minute = 60000 millis. This is basic math! getTimeInMillis() returns the current time as UTC milliseconds from the epoch. How do you convert milliseconds to minutes and seconds?Convert Milliseconds to minutes using the formula: minutes = (milliseconds/1000)/60). Convert Milliseconds to seconds using the formula: seconds = (milliseconds/1000)%60). How do you convert milliseconds to seconds in node JS?floor((ms/1000/60) << 0), sec = Math. floor((ms/1000) % 60); console. log(min + ':' + sec); |