How To Get All Dates Between Two Dates in Moment JS?

JavaScript , Jquery

Nicesnippets

5436

17-06-2021


Hello Friends,

Now let's see example of how to fetch all dates between two dates using moment js. We will talk about get all dates between two dates in moment js. In this articel, I am going to show you how to get all dates between two dates in moment js.

In this blog, I will learn you get all dates between two dates in moment js. Here I will give you step by step explain moment get all days between two dates.

So let's see the bellow example:

Example


<!DOCTYPE html>

<html>

<head>

<title>jquery moment example - NiceSnippets.com</title>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" crossorigin="anonymous"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" crossorigin="anonymous"></script>

</head>

<body>

<h1>jquery moment example - NiceSnippets.com</h1>

</body>

<script type="text/javascript">

var getDaysBetweenDates = function(startDate, endDate) {

var now = startDate.clone(), dates = [];

while (now.isSameOrBefore(endDate)) {

dates.push(now.format('MM/DD/YYYY'));

now.add(1, 'days');

}

return dates;

};

var startDate = moment('2021-01-01');

var endDate = moment('2021-01-06');

var dateList = getDaysBetweenDates(startDate, endDate);

console.log(dateList);

</script>

</html>

Output

Array(6)

0: "01/01/2021"

1: "01/02/2021"

2: "01/03/2021"

3: "01/04/2021"

4: "01/05/2021"

5: "01/06/2021"

It will help you....