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

noblemfd's avatar

How to Resolve Invalid Date in JQuery Leave Calculation

I am working on HR Project using JQuery-UI Datepicker. I have these fields:

<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">
var holidayDays = ["2020-11-12", "2020-11-16", "2020-11-19", "2020-11-30", "2020-12-14", "2020-12-01", "2020-12-10"];
var startDate = new Date();
var endDate = new Date(startDate.setDate(startDate.getDate() + 1));

for (i = 0; i < holidayDays.length; i++) {
  var date = endDate.getDate();
  var month = endDate.getMonth() + 1; //Months are zero based
  var year = endDate.getFullYear();
  if ((month + '/' + date + '/' + year) === (holiday[i])) {
    endDate = new Date(endDate.setDate(endDate.getDate() + 1));
    if (endDate.getDay() == 6) {
      endDate = new Date(endDate.setDate(endDate.getDate() + 2));
    } else if (endDate.getDay() == 0) {
      endDate = new Date(endDate.setDate(endDate.getDate() + 1));
    }
  }
}

I want to get the resumption date (endDate) from commencement date (startDate) excluding holidays and weekends. So I tried to re-write the code since I have the datepickers as commencement_date and resumption_date

< script type = "text/javascript" >
    $(document).ready(function() {
      function() {
      $("#leave_days").on('keyup blur', function(e) {
        var cDate = document.getElementById("commencement_date").value;
        var rDate = document.getElementById("resumption_date").value;
        var startDate = new Date(cDate);
      }); <
    /script>

I tried to check from the alert using:

alert(startDate);

But I got this:

Invalid Date

How do I correct this?

And How do I incorporate commencement_date and resumption_date into:

var startDate = new Date();
var endDate = new Date(startDate.setDate(startDate.getDate() + 1));

for (i = 0; i < holidayDays.length; i++) {
  var date = endDate.getDate();
  var month = endDate.getMonth() + 1; //Months are zero based
  var year = endDate.getFullYear();
  if ((month + '/' + date + '/' + year) === (holiday[i])) {
    endDate = new Date(endDate.setDate(endDate.getDate() + 1));
    if (endDate.getDay() == 6) {
      endDate = new Date(endDate.setDate(endDate.getDate() + 2));
    } else if (endDate.getDay() == 0) {
      endDate = new Date(endDate.setDate(endDate.getDate() + 1));
    }
  }
}

NOTE: I have the date format as:

            $('.commencement_date').datepicker({
               dateFormat: 'dd-mm-yy',
0 likes
0 replies

Please or to participate in this conversation.