JavaScript Difference Between Two Dates In Minutes Example


Share:
Published By: Admin, Published On: Sep 02, 2021, Category: Javascript


Hello Friends,

Now let's see example of how to get difference between two dates in minutes example. We will use how to get difference between two dates in minutes in javascript. Here you will learn how to use javascript get difference between two dates in minutes.Let's get started with how to get difference between two dates in minutes in javascript.

Here i will give you many example how you can get difference between two dates in minutes javascript.

Example : 1
<!DOCTYPE html>
<html>
		<head>
				<meta charset="utf-8">
				<title>JavaScript Difference Between Two Dates in Minutes - MyWebtuts.com </title>
		</head>
		<body>
		<script type="text/javascript">
				function diff_minutes(dt2, dt1) 
				{
						var diff =(dt2.getTime() - dt1.getTime()) / 1000;
						diff /= 60;
						return Math.abs(Math.round(diff));
				}
				dt1 = new Date(2021,5,2);
				dt2 = new Date(2021,5,14);
				console.log(diff_minutes(dt1, dt2));
		</script>
		</body>
</html>
Output :
17280
Example : 2
<!DOCTYPE HTML>
<html>
		<head>
				<title>JavaScript Difference Between Two Dates in Minutes - MyWebtuts.com </title>
				<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
		</head>
		<body class="bg-dark">
				<div class="container mt-5">
						<div class="row">
								<div class="col-md-6 offset-md-3">
										<div class="card">
												<div class="card-header">
														<h5>JavaScript Difference Between Two Dates in Minutes</h5>
												</div>
												<div class="card-body">
														<p id = "emp1"></p>
														<button onClick = "cmp()" class="btn btn-primary mb-2">Check</button>
														<p id = "emp2"></p>
												</div>
										</div>
								</div>
						</div>
				</div>
		<script>
				var cmp1 = document.getElementById('emp1');
				var cmp2 = document.getElementById('emp2');
				var today = new Date();
				var newYear = new Date("01-01-2021");
				cmp1.innerHTML = "Today's Date= "+ today.toLocaleDateString()
						+ "<br> Calculate Date = " + newYear.toLocaleDateString();
				
				function cmp() {
						var dif = (newYear - today);
						var dif = Math.round((dif/1000)/60);
						cmp2.innerHTML = "Minutes left = " + dif;
				}
		</script>
		</body>
</html>
Output :
Minutes left = -278896

It will help you...

Share: