JavaScript Difference Between Two Dates In Hours Example

Sep 04, 2021 . Admin



Hello Friends,

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

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

Example : 1
<!DOCTYPE html>
<html>
		<head>
				<meta charset="utf-8">
				<title>JavaScript Difference Between Two Dates in Hours - MyWebtuts.com </title>
		</head>
		<body>
		<script type="text/javascript">
				const dateOne = "6 Apr, 2015 14:45";
				const dateTwo = "7 May, 2015 02:45";
				const dateOneObj = new Date(dateOne);
				const dateTwoObj = new Date(dateTwo);
				const milliseconds = Math.abs(dateTwoObj - dateOneObj);
				const hours = milliseconds / 36e5;

				console.log(hours);
		</script>
		</body>
</html>
Output :
732
Example : 2
<!DOCTYPE html>
<html>
		<head>
				<title>JavaScript Difference Between Two Dates in Hours - 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 Hours</h5>
												</div>
												<div class="card-body">
														<p id="cmp"></p>
												</div>
										</div>
								</div>
						</div>
				</div>
				<script>
						var emp1 = new Date("10 May, 2021 14:45");
						var emp2 = new Date("2 March, 2021 14:45");
						var cmp = (emp1-emp2)/36e5;
						document.getElementById("cmp").innerHTML = cmp;
				</script>
		</body>
</html>
Output :
1656

It will help you...

#Javascript