Divyesh
Divyesh

Reputation: 39

How to show dynamic dates with month name of current month and previous month in momentjs?

how can I get all the dates of the current and previous month in moment.js? I was able to get the months but unable to find the dates of months The below code if for getting months is there any similar code for getting dates also?

var labels = moment.monthsShort();

i want to show something like this image

Upvotes: 2

Views: 477

Answers (1)

Frenchy
Frenchy

Reputation: 17007

//sample to display february days
var months = moment.monthsShort();

var datesofmonth = createAllDatesOfMonth(months[1], 2021)

console.log(datesofmonth);


function createAllDatesOfMonth(month, year) {
  let year_month = year + "-" + month;
  let nbdays = moment(year_month, "YYYY-MMM").daysInMonth();
  return Array.from(Array(nbdays).keys()).map((d) => month + " " + (d + 1));
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.5.1/moment.min.js"></script>

Upvotes: 2

Related Questions