JQuery - Uncaught TypeError: startDate.getDate is not a function

前提是你 提交于 2021-02-11 12:50:10

问题


Using JQuery-UI datepicker, I am trying to calculate resumption date (end date) from commencement date (start date) and leave days excluding holidays and weekends. This event will be triggered when #leave_days is onkeyup.

<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" >
  $(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");
      dte.setDate(dte.getDate() + periodval);
      endDate.datepicker("setDate", dte);

      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 ((year + '-' + month + '-' + date) === (holidayDays[i])) {
          endDate = new Date(endDate.setDate(endDate.getDate() + 1));
          endDate.datepicker("setDate", dte);
          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));
          }
        }
      }

    });

    $('.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>

I would like to find the resumption_date (endDate), based on a given number of days (leave_days) and commencement_date (StartDate). It should exclude weekends and holidays.

holidays is holidayDays

holidayDays = ["2020-11-12", "2020-11-16", "2020-11-19", "2020-11-30", "2020-12-14", "2020-12-01", "2020-12-10"]

When #leave_days on keyup, I got this error:

Uncaught TypeError: startDate.getDate is not a function

How do I resolve this?

来源:https://stackoverflow.com/questions/64804193/jquery-uncaught-typeerror-startdate-getdate-is-not-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!