Moment JS Get All Dates in Month Example

By Hardik Savani January 13, 2021 Category : jQuery

Hi,

I am going to explain you example of moment get all dates in month. step by step explain how to get all dates in a month in moment js. you will learn jquery moment get dates in month. This article will give you simple example of get all dates in month moment js.

Here, i will give you simple example of jquery moment js get all dates in a month.

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 getDaysOfMonth = function(year, month) {

var monthDate = moment(year+'-'+month, 'YYYY-MM');

var daysInMonth = monthDate.daysInMonth();

var arrDays = [];

while(daysInMonth) {

var current = moment().date(daysInMonth);

arrDays.push(current.format('MM-DD-YYYY'));

daysInMonth--;

}

return arrDays;

};

var dateList = getDaysOfMonth(2021, 01);

console.log(dateList);

</script>

</html>

Output:

0: "01-31-2021"

1: "01-30-2021"

2: "01-29-2021"

3: "01-28-2021"

4: "01-27-2021"

5: "01-26-2021"

6: "01-25-2021"

7: "01-24-2021"

8: "01-23-2021"

9: "01-22-2021"

10: "01-21-2021"

11: "01-20-2021"

...

26: "01-05-2021"

27: "01-04-2021"

28: "01-03-2021"

29: "01-02-2021"

30: "01-01-2021"

I hope it can help you...

Tags :
Shares