6

Javascript Convert Date and Time Example

 1 year ago
source link: https://www.laravelcode.com/post/javascript-convert-date-and-time-example
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Javascript Convert Date and Time Example

  158 views

  5 months ago

Javascript

In this article, we will share with you how to convert date and time in various examples. in the web application, we will be done much functionality with javascript and it is very easy to do from the client-side. many times we will need to convert date and time in many formats. here we will be going to share with you some basic needful dates and times converted with hours, minutes, mili-seconds, etc...

Example - 1 Convert hours to minutes :

Here, we are creating one javascript function and write into it how to convert hours to minutes in javascript.

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

Example - 2 Convert hours to seconds :

In this example, we will create one javascript function that returns us seconds of the passed hours value in the function.

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

Example - 3 Convert hours to milliseconds :

In this above example, we can see how to convert hours to milliseconds in javascript.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Convert hours to milliseconds - HackTheStuff<</title>
</head>
<body>
  	<script type = "text/javascript">
	    function convertHourstoMilliseconds(hours) {
	      	return Math.floor(hours * 60 * 60 * 1000);
	    }
	    var milliseconds = convertHourstoMilliseconds(2);
	    document.write("Converting hours to milliseconds is :- " + milliseconds);
  	</script>  
</body>
</html>

Example - 4 Convert minutes to seconds :

In this example, we will see how to convert minutes to seconds in javascript.

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

Example - 5 Convert minutes to milliseconds :

In this example, we see how to convert minutes to milliseconds in the javascript example.

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

Example - 6 Convert seconds to milliseconds :

In this example, we see how to convert seconds to milliseconds in javascript.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Convert seconds to milliseconds - HackTheStuff</title>
</head>
<body>
  	<script type = "text/javascript">
	    function convertSecondstoMilliSeconds(seconds) {
	      	return Math.floor(seconds * 1000);
	    }
	    var secondsToMilliSeconds = convertSecondstoMilliSeconds(2);
	    document.write("Converting seconds to milliseconds is :- " + secondsToMilliSeconds);
  	</script>  
</body>
</html>

Example - 7 Convert date to milliseconds :

In this example, we see how to convert date to milliseconds in javascript.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Convert date to milliseconds - HackTheStuff</title>
</head>
<body>
  	<script type = "text/javascript">
    	var date = new Date(); 
    	var milliseconds = date.getTime(); 
    	document.write("Converting date to milliseconds is :- " + milliseconds);
  	</script>  
</body>
</html>

Example - 8 Convert seconds to hh mm ss :

In this example, we see how to convert seconds to hh mm ss format.

<html lang="en">
<head>
<meta charset="utf-8">
<title>Convert seconds to hh mm ss - HackTheStuff</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);
    	document.write("Converting to seconds in HH-MM-SS format :- " + secondsTo);
  	</script>  
</body>
</html>

We hope you like this small article and it will be help you a lot.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK