In this post we will find all the dates between two date in JavaScript. We will create two variables start date and end date in YYYY-MM-DD format and will find all the date between two dates.
Start and End Dates.
var start_date = new Date(“2021-03-01”),
var end_date = new Date(“2021-03-27”);
Create a function to find all dates.
In this step we will create the function that will return the all the date between start Date and end Date.
<html>
<head>
<script>
var startDate = new Date(“2021-03-01”); //YYYY-MM-DD
var endDate = new Date(“2021-03-22”); //YYYY-MM-DD
var allDate = function(start_date, end_date) {
var date_range = new Array();
var st_date = new Date(start_date);
while (st_date <= end_date) {
let month = (“0” + (st_date.getMonth() + 1)).slice(-2);
let day = (“0” + st_date.getDate()).slice(-2);
let date = [st_date.getFullYear(), month, day].join(“-“);
date_range.push(date);
st_date.setDate(st_date.getDate() + 1);
}
return date_range;
}
var dateArr = allDate(startDate, endDate);
// Output
document.write(“<html><body><span>Start Date: ” + startDate + “</span><br>”);
document.write(“<span>End Date: ” + endDate + “</span><br>————<br>”);
document.write(“<span>All Date</span><br>———<br>”)
for (var i = 0; i < dateArr.length; i++) {
document.write(“<span>” + dateArr[i] + “</span><br>”);
}
document.write(“</body></html>”);
</script>
</head>
<body>
</body>
</html>
The post Find All Date Between Two Dates In JavaScript. appeared first on PHPFOREVER.