Google Sheets – Count Number of Fridays in a Specific Month

google sheets

I would like to find the number of Fridays for a specific month via function in Google Spreadsheets.

For example, for January 2014 the value would be 5 and for February 2014 the value would be 4.

How can I do that?

Best Answer

This is how to do that with Google Apps Script.

Code

function specificDays(dayName, monthName, year) {
  // set names
  var monthNames = ["January", "February", "March", 
    "April", "May", "June",
    "July", "August", "September", 
    "October", "November", "December"
  ];
  var dayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", 
    "Thursday", "Friday", "Saterday"
  ];

  // change string to index of array
  var day = dayNames.indexOf(dayName);
  var month = monthNames.indexOf(monthName)+1;

  // determine the number of days in month
  var daysinMonth = new Date(year, month, 0).getDate();

  // set counter
  var sumDays=0;

  // iterate over the days and compare to day
  for(var i=1; i<=daysinMonth; i++) {
    var checkDay = new Date(year, month-1, parseInt(i)).getDay();    
    if(day == checkDay) {
      sumDays ++;
    }
  }

  // show amount of day names in month
  return sumDays;
}

Screenshot

enter image description here

Remarks

Add the script via Tools>Script editor in the menu. Save the script and you're on the go !!

Example

I've created an example file for you: Amount of Day Names in Month