Skip to content

Instantly share code, notes, and snippets.

@biplab3
Created March 21, 2021 05:10
Show Gist options
  • Save biplab3/4a7813e7e3d2dc5088ee9562d72e5334 to your computer and use it in GitHub Desktop.
Save biplab3/4a7813e7e3d2dc5088ee9562d72e5334 to your computer and use it in GitHub Desktop.
<html>
<head>
<title>ajax example</title>
<link rel="stylesheet" href="bootstrap.css" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="bootstrap-theme.css" crossorigin="anonymous">
<style>
.container{
width:70%;
height:30%;
padding:20px;
}
</style>
</head>
<body>
<div class="container">
<h3 align="center">Calculate Javascript Difference between Two Dates in Days</h3>
<br/><br/><br/>
<div class="form-group row">
<label class="control-label col-sm-2">Start Date:</label>
<div class="col-sm-2">
<input type="date" id="start_date">
</div>
<label class="control-label col-sm-2">End Date:</label>
<div class="col-sm-2">
<input type="date" id="end_date" onchange="getDays()">
</div>
<label class="control-label col-sm-2">Days:</label>
<div class="col-sm-2">
<input type="text" id="days">
</div>
</div>
<br/><br>
</form>
</div>
<script src="jquery-3.2.1.min.js"></script>
<script src="bootstrap.min.js"></script>
<script>
//get the days between two dates
function getDays(){
var start_date = new Date(document.getElementById('start_date').value);
var end_date = new Date(document.getElementById('end_date').value);
//Here we will use getTime() function to get the time difference
var time_difference = end_date.getTime() - start_date.getTime();
//Here we will divide the above time difference by the no of miliseconds in a day
var days_difference = time_difference / (1000*3600*24);
//alert(days);
document.getElementById('days').value = days_difference;
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment