0

I'm nearly finished with the script to automate the bulletin for my school. The last piece is to add the birthdays to the end of the bulletin. The user will have already entered a date for the bulletin, resulting in most of the bulletin being written. I want to use the entered date to compare against student birthdays in a sheet called Birthdays. I attempted to translate each birthday to the integer value of the day of the year based on this answer.

It is supposed to check the day, even though the year may be different and add the student's name to the next line.

Here is the code for this part so far:

    var bDayHeader= "Birthdays"
bulletin.getRange(nextBullRow,1).setValue(bDayHeader);
nextBullRow+=2;
var birthdays = ss.getSheetByName("Birthdays");
var lastBdayRow = birthdays.getLastRow();
for(var i=2; i<=lastBdayRow; i++) {
  var bRow = birthdays.getRange(i,1,1,3).getValues();
  var bDay = new Date(bRow[0][0]);
  var bDayStudent = bRow[0][2];
  if(compareDate.getMonth() == bDay.getMonth() && compareDate.getDate() == bDay.getDate()){
    Logger.log(bDayStudent);
    bulletin.getRange(nextBullRow,1).setValue(bDayStudent);
    bulletin.getRange(nextBullRow,1).setFontWeight("normal");
    nextBullRow++ ;          
  }
}
Rob Campbell
  • 63
  • 11

1 Answers1

1

I would do something like

if(compareDate.getMonth() == bDay.getMonth() && compareDate.getDate() == bDay.getDate()) {
  // its the same day of the month.
}

This obviously is mean to anyone born on the 29th of February. :D

You can read more about the methods available to Date objects on the MDN Date reference.

tehhowch
  • 9,645
  • 4
  • 24
  • 42
  • You again!! :D That simplifies it greatly. I also adapted some of your earlier solutions to make it work better and have more efficient code. Specifically, using an array to capture the data. This will make someone's job easier/faster/better. You made a lot of key fixes for me. Thanks – Rob Campbell Mar 16 '18 at 05:22