0

I want to enable only the last Friday in each month in my datepicker. In another example I found a code-snippet but it doesn't work for me. Maybe my picker uses other attribute names?

How I can do this?

var monthDays = new Date(date.getFullYear(),date.getMonth() + 1,0).getDate(); 
var day = date.getDay(); 
return [( (day==5 && date.getDate() > monthDays - 7 )), '']; 
}

Here is the codepen for the tracker

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Chris
  • 23
  • 5
  • 1
    Just curious. Wouldn't a better (i.e. more efficient) strategy be to calculate the date of the last Friday once (e.g. `let lastFridayDate = d.getDate() - ((d.getDay() + 2) %7)`), then disable any date for the month that isn't that date (e.g. `return [date.getDate() == lastFriday, '']`)? It seems to me that the current strategy calculates *monthDays* once for every day of the month. – RobG Aug 09 '22 at 08:38

1 Answers1

1

Your snippet is correct, but you need to place it in the constructor like this:

jQuery('#datetimepicker').datetimepicker({
 allowTimes:[
  '10:00', '11:00', '12:00','14:00'
 ],
  beforeShowDay: function(date) {
      const monthDays = new Date(date.getFullYear(),date.getMonth() + 1,0).getDate();
      const day = date.getDay();
      return [( (day==5 && date.getDate() > monthDays - 7 )), ''];
    }
});
Jeroen Verfaillie
  • 611
  • 1
  • 4
  • 16