How to get two dates between days in JavaScript
In this tutorial, we will learn How to get two dates between days in JavaScript. we have explained the simple way to get the different days between dates. We will use date() and math() function to get the number of days between dates. Date() And Math() functions are the JavaScript default function, and we are using them to get the number of days between two dates. we will create a differenceDay() function, which we will pass two arguments, Like first_date and last_date.
Also read: How to get all dates between two dates using moment js
First, we will define two dates (first_date, last_date) and then we will use start.getTime()- end.getTime() to determine the time difference between two dates. Second, we will determine the number of days between dates. Also, we do this by dividing the number of milliseconds(milliSecondBetween variable) in a day. And last, the total number of days between two dates will be returned.
How to get a number of days between two dates using JavaScript.
Example
<!DOCTYPE html>
<html>
<head>
<title>How to get two dates between days in JavaScript</title>
</head>
<body>
<h1>How to get two dates between days in JavaScript</h1>
<div id="displayDays"></div>
<script type="text/javascript">
var days = daysdifference('04/04/2022', '04/20/2022');
document.getElementById('displayDays').innerHTML += "Total Days:" + days;
function daysdifference(first_date, last_date) {
var start = new Date(first_date);
var end = new Date(last_date);
var milliSecondBetween = start.getTime() - end.getTime();
var days = milliSecondBetween / (1000 * 3600 * 24);
return Math.round(Math.abs(days));
}
</script>
</body>
</html>
Output:
How to get two dates between days in JavaScript
Total Days:16