0

I have this code through which I show a future working day, thus excluding weekends. I would also like to exclude personalized holiday dates, in which case the same thing happens (count ++, I guess).

For example, I would like to exclude these dates: ["2019-11-6", "2019-11-13"] How can I integrate this into the code?

I need this to show a future delivery date on a business day, excluding weekends and some custom dates.

I've read and tested countless similar questions here on StackOverflow, but I haven't found anything that works.

jQuery(function($) {
  var monthNames = ["gennaio", "febbraio", "marzo", "aprile", "maggio", "giugno", "luglio", "agosto", "settembre", "ottobre", "novembre", "dicembre"];
  var dayNames = ["domenica", "lunedì", "martedì", "mercoledì", "giovedì", "venerdì", "sabato"]

  var endDate = "",
    noOfDaysToAdd = 1,
    count = 0;
  var someDate = new Date();
  var numberOfDaysToAdd = noOfDaysToAdd;
  someDate.setDate(someDate.getDate());

  while (count < noOfDaysToAdd) {
    endDate = new Date(someDate.setDate(someDate.getDate() + 1));
    if (endDate.getDay() != 0 && endDate.getDay() != 6) {
      count++;
    }
  }

  $('#Date').html(dayNames[endDate.getDay()] + ' ' + endDate.getDate() + ' ' + monthNames[endDate.getMonth()] + ' ' + endDate.getFullYear());
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="Date"></div>

The code works well for the exclusion of weekends, but I would like help to add customized holiday dates as well.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Please refer this https://stackoverflow.com/questions/22065143/how-do-i-disable-public-holidays-in-a-jquery-ui-date-picker – jishan siddique Nov 05 '19 at 08:59
  • Thanks for immediate support, Rory McCrossan! Unfortunately though, although I did a lot of research and attempts, I couldn't integrate the code in the question you suggested with mine. Perhaps because that refers to a datepicker, or because of my incompetence :) Any other suggestions or practical examples? Thank you very much. – EnglishSound Nov 05 '19 at 09:39
  • I suggest taking a look at [Moment.js](https://momentjs.com/). – silentw Nov 05 '19 at 18:10
  • Thanks so much for the tip Silentw! Unfortunately I have already tried with Moment.js, but it is a bit too complicated for me, also because I would need only this function above in the whole site. Thanks anyway! – EnglishSound Nov 05 '19 at 18:21

1 Answers1

0

Thanks to Silentw for giving me the right direction! After several researches I realized that what was missing was just setting the numbers of the dates separated by commas. Now it seems to work perfectly. Thank you all for the support.

Here is the working code for those interested:


jQuery(function($) {
  var monthNames = ["gennaio", "febbraio", "marzo", "aprile", "maggio", "giugno", "luglio", "agosto", "settembre", "ottobre", "novembre", "dicembre"];
  var dayNames = ["domenica", "lunedì", "martedì", "mercoledì", "giovedì", "venerdì", "sabato"];
  var holidays = ["2019, 11, 8", "2019, 11, 11", "2019, 11, 12", "2019, 11, 13", "2019, 11, 14", "2019, 11, 15", "2019, 11, 18"];
  var endDate = "",
    noOfDaysToAdd = 3,
    count = 0;
  var someDate = new Date(new Date().toDateString());
  var numberOfDaysToAdd = noOfDaysToAdd;
  someDate.setDate(someDate.getDate());
  while (count < noOfDaysToAdd) {
    endDate = new Date(someDate.setDate(someDate.getDate() + 1));
    var isHoliday = holidays.find(holiday => endDate.getTime() == new Date(holiday).getTime());
    if (isHoliday) {
      console.log('holiday, skipping');
    } else if (endDate.getDay() != 0 && endDate.getDay() != 6) {
      count++;
    }
  }
$('#Date').html(dayNames[endDate.getDay()] + ' ' + endDate.getDate() + ' ' + monthNames[endDate.getMonth()]);   
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="Date"></div>

Added on behalf of OP

Dharman
  • 30,962
  • 25
  • 85
  • 135