Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

noblemfd's avatar

How to Calculate End Date from Start Date, days, weekend and holiday using JQuery

I am development an application HR Application using Laravel-5.8 and JQuery-ui datepicker.

I passed the National Holidays from the Controller to the view using JSON, and this is working perfectly:

I have these three fields:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="commencement_date" class="form-control commencement_date" placeholder="dd/mm/yyyy" readonly autocomplete="off" name="commencement_date">
<input type="text" id="leave_days" name="no_of_days" class="form-control no_of_days" placeholder="e.g. 10" maxlength="3" onkeyup="checkScore(this.value)">
<input type="text" id="resumption_date" class="form-control resumption_date" placeholder="dd/mm/yyyy" readonly autocomplete="off" name="resumption_date">
< script type = "text/javascript" >
  var holidayDays = [];
$(document).ready(function() {
  $(document).on('change', '#leave_type', function() {
    var air_id = $(this).val();

    var a = $(this).parent();
    var op = "";

    $.ajax({
      type: 'get',
      url: '{{ route('
      get.leavecounts.all ') }}',
      data: {
        'id': air_id
      },
      dataType: 'json', //return data will be json
      // console.log("Its Change !");
      success: function(data) {
        holidayDays = data.nationalholidays;
      },
      error: function() {

      }
    });
  });
}); <
/script>

#leave_type onchange loads the national_holidays. In the console, the National Holidays come in this format:

(7) ["2020-11-12", "2020-11-16", "2020-11-19", "2020-11-30", "2020-12-14", "2020-12-01", "2020-12-10"]
0: "2020-11-12"
1: "2020-11-16"
2: "2020-11-19"
3: "2020-11-30"
4: "2020-12-14"
5: "2020-12-01"
6: "2020-12-10"
length: 7
__proto__: Array(0)

Using the code below, On #leave_days keyup, I've been able to calculate Resumption Date from Commencement Date + Leave Days. But this does not take care of National Holidays and Weekends.

< script type = "text/javascript" >
  $(document).ready(function() {
    $("#leave_days").on('keyup blur', function(e) {
      var periodval = parseInt($("#leave_days").val());
      holidayDays = holidayDays;
      console.log(holidayDays);;
      var startDate = $('.commencement_date');
      var endDate = $('.resumption_date');

      var dte = startDate.datepicker("getDate");

    });

    $('.commencement_date').datepicker({
      dateFormat: 'dd-mm-yy',
      changeMonth: true,
      changeYear: true,
      showAnim: 'slideDown',
      duration: 'fast',
      minDate: +1,
      setDate: new Date(),
      beforeShowDay: $.datepicker.noWeekends,
      yearRange: new Date().getFullYear() + ':' + new Date().getFullYear(),
    }).datepicker('setDate', '1');

    $('.resumption_date').datepicker({
      dateFormat: 'dd-mm-yy',
      changeMonth: true,
      changeYear: true,
      showAnim: 'slideDown',
      duration: 'fast',
      minDate: +1,
      beforeShowDay: $.datepicker.noWeekends,
      yearRange: new Date().getFullYear() + ':' + new Date().getFullYear(),
      enableOnReadonly: true,
      beforeShow: function(i) {
        if ($(i).attr('readonly')) {
          return false;
        }
      }
    });
  }); <
/script>

The issue now is, how do I On #leave_days keyup calculate the Resumption Date from Commencement Date LeaveDays, NationalHoliday and Weekend?

Thanks

0 likes
0 replies

Please or to participate in this conversation.