-2

I need to find how many weeks are there in a month. For example, start date is (24-04-2021) and the end date is (01-05-2021) April month has five weeks but we are getting only 4 weeks as output. here is what I have tried so far

function getWeekOfMonth(date) {
  const startWeekDayIndex = 1; // 1 MonthDay 0 Sundays
  const firstDate = new Date(date.getFullYear(), date.getMonth(), 1);
  const firstDay = firstDate.getDay();

  let weekNumber = Math.ceil((date.getDate() + firstDay) / 7);
  if (startWeekDayIndex === 1) {
    if (date.getDay() === 0 && date.getDate() > 1) {
      weekNumber -= 1;
    }

    if (firstDate.getDate() === 1 && firstDay === 0 && date.getDate() > 1) {
      weekNumber += 1;
    }
  }
  return weekNumber;
}
DineshMsd
  • 80
  • 9
  • Please clarify the input and output. Input seems to be a specific date, and you're asking which full week of a month it is in? – ChrisG May 30 '22 at 14:02
  • https://stackoverflow.com/questions/2483719/get-weeks-in-month-through-javascript – vfioox May 30 '22 at 14:04

0 Answers0