skip to Main Content

I use jQuery UI DatePicker and was able to disable weekends and holidays there (custom list). Now I need to count the number of working days in a month without taking into account the days off before the selected date.

That is, I choose January 20. 1-8 numbers and 14-15 are non-working. I need to count the rest. Please tell me exactly how I can do this.

enter image description here

I only understood how to calculate the days between two dates. But this is absolutely not the case.

  let dt1 = new Date(fromdate);
  let dt2 = new Date(todate);
 
  let time_difference = dt2.getTime() - dt1.getTime();
  let result = time_difference / (1000 * 60 * 60 * 24);

2

Answers


  1. Chosen as BEST ANSWER

    Sorry it took so long and thanks for your response. It's obvious and that's not what I was asking, but still.

    This is the function I got:

        function calculateWorkingDays() {
        let datePickerDw = $("#input-date-of-dismissal-td").datepicker("getDate");
    
        // Getting the selected date from the date selection panel
        let firstDayOfMonth = datePickerDw;
        let selectedDate = datePickerDw;
    
        let yyyy = new Date().getFullYear();
    
        // Holiday Array
        let unrecordedDays = [
            new Date(yyyy, 0, 2).getTime(),
            new Date(yyyy, 0, 3).getTime(),
            new Date(yyyy, 0, 4).getTime(),
            new Date(yyyy, 0, 5).getTime(),
            new Date(yyyy, 0, 6).getTime(),
            new Date(yyyy, 1, 23).getTime(),
            new Date(yyyy, 1, 24).getTime(),
            new Date(yyyy, 2, 8).getTime(),
            new Date(yyyy, 4, 1).getTime(),
            new Date(yyyy, 4, 8).getTime(),
            new Date(yyyy, 4, 9).getTime(),
            new Date(yyyy, 5, 12).getTime(),
            new Date(yyyy, 10, 6).getTime()
        ];
    
        // Working day counter with weekend and holiday check.
        let start = new Date(firstDayOfMonth.getFullYear(), firstDayOfMonth.getMonth(), 1);
        let end = new Date(selectedDate.getFullYear(), selectedDate.getMonth(), selectedDate.getDate());
        let workingDays = -1;
    
        while (start <= end) {
            if (start.getDay() !== 0 && start.getDay() !== 6 && !unrecordedDays.includes(start.getTime())) {
                workingDays++;
            }
            start.setDate(start.getDate() + 1);
        }
        $("#numbers-days-worked-td").val(workingDays);
        return workingDays;
    }
    

    Launch this function using the method:

    onSelect: function() {
            calculateWorkingDays();
        }
    

    Example:

        $("#input-date-of-dismissal-td").datepicker({
        dateFormat: "dd.mm.yy",
        minDate: "+4d",
        firstDay: 1,
        monthNames: ["Январь", "Февраль", "Март", "Апрель", "Май", "Июнь", "Июль", "Август", "Сентябрь", "Октябрь", "Ноябрь", "Декабрь"],
        dayNamesMin: ["Вс", "Пн", "Вт", "Ср", "Чт", "Пт", "Сб"],
        beforeShowDay: $.datepicker.noWeekendsAndHoliday,
        onSelect: function() {
            calculateWorkingDays();
        }
    }).datepicker();
    

  2. jQuery UI takes saturday = 6 and sunday = 0 as numeric value. SO you can ignore that numeric value inside for loop to calculate number of days.

    Example:

    let dt1 = new Date(fromdate);
    let dt2 = new Date(todate);
    for (var i = dt1; i <= dt2; i++)) {
      if (i.getDay() != 6 && i.getDay() != 0) {
        days = days + 1;
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search