-1

How to get the quantity of a given weekday in a particular month? For example, get the quantity of Sundays in Jan 2023 or get the quantity of Tuesday in Feb 2023

  • Does this answer your question? [Calculate number of specific weekdays between dates](https://stackoverflow.com/questions/25562173/calculate-number-of-specific-weekdays-between-dates) – Heretic Monkey Jun 24 '23 at 01:16

1 Answers1

1
function getWeekdayCount(year, month, weekday) {
  const firstDay = new Date(year, month, 1);
  const lastDay = new Date(year, month + 1, 0);
  const count = Math.floor((lastDay.getDate() - firstDay.getDate() + (firstDay.getDay() + 7 - weekday) % 7) / 7);
  
  return count;
}

// Example usage:
const year = 2023;
const month = 0; 
const weekday = 0; 

const count = getWeekdayCount(year, month, weekday);
console.log(count); // Output: Number of occurrences of the weekday in the specified month

(lastDay.getDate() - firstDay.getDate() + (firstDay.getDay() + 7 - weekday) % 7) / 7 calculates the number of occurrences of the specified weekday in the month.

guy mograbi
  • 27,391
  • 16
  • 83
  • 122