Get All Dates Between Two Dates in Moment JS

By Hardik Savani January 12, 2021 Category : jQuery

Hi,

This article is focused on moment get list of dates between two. if you want to see example of get all dates between two dates moment js then you are a right place. i explained simply about moment get all days between two dates. step by step explain moment get all days between two dates.

Here, i will give you simple example of jquery moment js get all dates between two dates.

let's see both example with output:

Example:

<!DOCTYPE html>

<html>

<head>

<title>jquery moment example - ItSolutionStuff.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 - ItSolutionStuff.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-02');

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

var dateList = getDaysBetweenDates(startDate, endDate);

console.log(dateList);

</script>

</html>

Output:

Array(11)

0: "01/02/2021"

1: "01/03/2021"

2: "01/04/2021"

3: "01/05/2021"

4: "01/06/2021"

5: "01/07/2021"

6: "01/08/2021"

7: "01/09/2021"

8: "01/10/2021"

9: "01/11/2021"

10: "01/12/2021"

I hope it can help you...

Tags :
Shares