简体   繁体   English

使用jQuery日期选择器减去日期

[英]subtract dates using jquery date picker

Currently working on jquery clone and datepicker where it will calulate the dates and it will check the overlaping dates. 当前在jquery clone和datepicker上工作,它将计算日期并检查重叠的日期。 But if the click the less button the code works perfectly it was removing the row but it was not detecting the dates which was there in the cloned div. 但是,如果单击“较少”按钮,则代码可以正常运行,它可以删除行,但不能检测到克隆的div中的日期。 For example when the user select the date 10-01-1990 & 10-01-1995 for this the total was Total work experience 5 years 0 Months & in the cloned div if the user gave 10-01-1996 & 10-01-2015 so the total was Total work experience 25 years 0 Months. 例如,当用户为此选择日期10-01-199010-01-1995时 ,如果用户给出10-01-199610-01-,则总计为5年0个月的总工作经验 &在克隆的div中。 2015年的总人数为25年0个月的总工作经验。 If the user click the lessbtn button the cloned one will remove but the total year was not detecting. 如果用户单击lessbtn按钮,则克隆的将被删除,但未检测到整个年份。

Here is the jquery code 这是jQuery代码

$(document).on('change', ".datepicker", function (){
        var valid=true;
        $.each($('.datepicker'),function(){
            if($(this).val()=="")
            {
                valid=false;
                return false;
            }
         });
         if(valid)
         {
            var dateStart=[];
            var dateEnd=[];
            $.each($('.datepicker'),function(){
                if($(this).hasClass('startDate'))
                    dateStart.push($(this).val())
                else
                    dateEnd.push($(this).val())
            });

            $.each($(dateStart),function(key,value){
                var x = dateStart[key].split("-");
                var y = dateEnd[key].split("-");

                var failed = false;
                var fromdate = new Date(x[2], x[0] - 1, x[1]);
                var todate = new Date(y[2], y[0] - 1, y[1]);
                var locDiffDays = parseInt((todate.getTime() - fromdate.getTime()) / (1000 * 60 * 60 * 24));

                console.log(x);
                console.log(y);
                console.log(fromdate);
                console.log(todate);
                console.log(locDiffDays);

                if(locDiffDays<0){
                        alert("To date " + dateEnd[key] + " should be greater then from date " + dateStart[key] );
                        console.log("invalid from and to dates"); failed = true; return false;
                }

                if(dateStart[key-1]){
                    var x1 = dateStart[key-1].split("-");
                    var y1 = dateEnd[key-1].split("-");

                    var fromdate1 = new Date(x1[2], x1[0] - 1, x1[1]);
                    var todate1 = new Date(y1[2], y1[0] - 1, y1[1]);
                    var locDiffDays1 = parseInt((todate1.getTime() - fromdate1.getTime()) / (1000 * 60 * 60 * 24));
                    console.log(x1);
                    console.log(y1);
                    console.log(fromdate1);
                    console.log(todate1);
                    console.log(locDiffDays1);

                    var locDiffDays2 = parseInt((fromdate.getTime() - fromdate1.getTime()) / (1000 * 60 * 60 * 24));
                    var locDiffDays3 = parseInt((todate.getTime() - todate1.getTime()) / (1000 * 60 * 60 * 24));
                    var locDiffDays4 = parseInt((fromdate.getTime() - todate1.getTime()) / (1000 * 60 * 60 * 24));

                    console.log("locDiffDays2: " + locDiffDays2);
                    if(locDiffDays2<0){
                        alert("From date " + dateStart[key] + " should be greater than previous from date " + dateStart[key-1] );
                        console.log("invalid from dates"); failed = true; return false;
                    }

                    if(locDiffDays3<0){
                        alert("To date " + dateStart[key] + " should be greater than previous To date " + dateStart[key-1] );
                        console.log("invalid from dates"); failed = true; return false;
                    }

                    if(locDiffDays4<0){
                        alert("From date " + dateStart[key] + " should be greater than previous To date " + dateEnd[key-1] );
                        console.log("invalid from dates"); failed = true; return false;
                    }
                }

                if(key == dateStart.length-1 && !failed){
                    var firstDate = dateStart[0].split('-');
                    firstDate = new Date(firstDate[2], firstDate[0] - 1, firstDate[1]);
                    var lastDate = dateEnd[dateEnd.length-1].split('-');
                    lastDate = new Date(lastDate[2], lastDate[0] - 1, lastDate[1]);
                    console.log(lastDate);
                    console.log(firstDate);
                    //var diffYears = (lastDate.getTime() - firstDate.getTime()) / (1000 * 60 * 60 * 24 * 365);
                    var diffMonths = monthDiff(firstDate, lastDate);
                    //diffYears = parseInt(''+diffYears);

                    var diffYears = diffMonths/12;
                    diffYears = parseInt(''+diffYears);

                    diffMonths = diffMonths - (diffYears * 12)

                    document.getElementById("txt_expy").innerHTML = diffYears + " years";
                    document.getElementById("txt_expm").innerHTML = diffMonths + " Months";
                }

            });

Kindly please suggest me. 请给我建议。

I am confused here do i want to put any count or something so when the user click the less button the count should reduce one and the value should get detected. 我对这里是否要输入任何计数感到困惑,所以当用户单击“较少”按钮时,计数应减少1,并且应该检测到该值。 Kindly give me tips here 请在这里给我提示

Hereis the fiddle Link 这是小提琴链接

Thanks & Regards 感谢和问候

Your issue is you are not calling any logic to update the difference while removing the cloned object. 您的问题是在删除克隆的对象时您没有调用任何逻辑来更新差异。 You wrote code for updating the difference only in datepicker.change event. 您编写了仅在datepicker.change事件中用于更新差异的代码。

Demo 演示

Updated code: 更新的代码:

$(document).on('change', ".datepicker", function() {
  updateDiff()
});
$(document).on('click', ".btn_less1", function() {
  var len = $('.cloned-row3').length;
  if (len > 1) {
    $(this).closest(".btn_less1").parent().parent().parent().remove();
    updateDiff();
  }
});



function updateDiff() {
  var valid = true;
  $.each($('.datepicker'), function() {
    if ($(this).val() == "") {
      valid = false;
      return false;
    }
  });
  if (valid) {
    var dateStart = [];
    var dateEnd = [];
    $.each($('.datepicker'), function() {
      if ($(this).hasClass('startDate')) dateStart.push($(this).val())
      else dateEnd.push($(this).val())
    });

    $.each($(dateStart), function(key, value) {
      var x = dateStart[key].split("-");
      var y = dateEnd[key].split("-");

      var failed = false;
      var fromdate = new Date(x[2], x[0] - 1, x[1]);
      var todate = new Date(y[2], y[0] - 1, y[1]);
      var locDiffDays = parseInt((todate.getTime() - fromdate.getTime()) / (1000 * 60 * 60 * 24));

      if (locDiffDays < 0) {
        alert("To date " + dateEnd[key] + " should be greater then from date " + dateStart[key]);
        console.log("invalid from and to dates");
        failed = true;
        return false;
      }

      if (dateStart[key - 1]) {
        var x1 = dateStart[key - 1].split("-");
        var y1 = dateEnd[key - 1].split("-");

        var fromdate1 = new Date(x1[2], x1[0] - 1, x1[1]);
        var todate1 = new Date(y1[2], y1[0] - 1, y1[1]);
        var locDiffDays1 = parseInt((todate1.getTime() - fromdate1.getTime()) / (1000 * 60 * 60 * 24));

        var locDiffDays2 = parseInt((fromdate.getTime() - fromdate1.getTime()) / (1000 * 60 * 60 * 24));
        var locDiffDays3 = parseInt((todate.getTime() - todate1.getTime()) / (1000 * 60 * 60 * 24));
        var locDiffDays4 = parseInt((fromdate.getTime() - todate1.getTime()) / (1000 * 60 * 60 * 24));

        console.log("locDiffDays2: " + locDiffDays2);
        if (locDiffDays2 < 0) {
          alert("From date " + dateStart[key] + " should be greater than previous from date " + dateStart[key - 1]);
          console.log("invalid from dates");
          failed = true;
          return false;
        }

        if (locDiffDays3 < 0) {
          alert("To date " + dateStart[key] + " should be greater than previous To date " + dateStart[key - 1]);
          console.log("invalid from dates");
          failed = true;
          return false;
        }

        if (locDiffDays4 < 0) {
          alert("From date " + dateStart[key] + " should be greater than previous To date " + dateEnd[key - 1]);
          console.log("invalid from dates");
          failed = true;
          return false;
        }
      }

      if (key == dateStart.length - 1 && !failed) {
        var firstDate = dateStart[0].split('-');
        firstDate = new Date(firstDate[2], firstDate[0] - 1, firstDate[1]);
        var lastDate = dateEnd[dateEnd.length - 1].split('-');
        lastDate = new Date(lastDate[2], lastDate[0] - 1, lastDate[1]);
        console.log(lastDate);
        console.log(firstDate);
        //var diffYears = (lastDate.getTime() - firstDate.getTime()) / (1000 * 60 * 60 * 24 * 365);
        var diffMonths = monthDiff(firstDate, lastDate);
        //diffYears = parseInt(''+diffYears);

        var diffYears = diffMonths / 12;
        diffYears = parseInt('' + diffYears);

        diffMonths = diffMonths - (diffYears * 12)

        document.getElementById("txt_expy").innerHTML = diffYears + " years";
        document.getElementById("txt_expm").innerHTML = diffMonths + " Months";
      }

    });

  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM