Jquery Datepicker does not set backdate value

Here is the problem, I have two datefields, when I select a date in the first field it sets another datefield value, it works correctly when I select later dates like if I select todays date which is 10th March then it sets another datefield properly, If I now select 14th march it works, if 16th selected it works now if I select go back and select less previous dates like 15th, 14th or 10th then it either the date assigned is wrong or it not works here is the code. Also I 'm trying to set the value which is friday and that too of last 8th week please help me with the code.

function b(entityName) {

  var friday = $('#AdDate').datepicker('getDate');
  friday.setDate(friday.getDate() - 51 - (friday.getDay() || 7));
  //Here I 'm trying to set the value which is Friday that too of previous 8th week.

  $(entityName).val();
  $(entityName).datepicker('setDate', '');
  $(entityName).datepicker('setDate', friday);
  //sets minDate to dt1 date + 1
  $(entityName).datepicker('option', 'minDate', friday);

}


$("#AdDate").datepicker({
  showOn: 'both',
  buttonImage: 'http://theonlytutorials.com/demo/x_office_calendar.png',
  buttonImageOnly: true,
  changeMonth: true,
  changeYear: true,
  showAnim: 'slideDown',
  duration: 'fast',
  dateFormat: "dd-M-yy",
  minDate: 0,
  onSelect: function(date) {
    //debugger;
    b("#ColorReleaseDateID"); //Please ignore the parameters.
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" rel="stylesheet"/>
<input type="text" id="AdDate"/>
<input type="text" id ="ColorReleaseDateID"/>