我想用javascript

时间:2018-10-29 05:01:37

标签: javascript php jquery ajax xampp

我已经创建了响应式网页,以某种计算方式提交了带有日期的数据,以给出两个日期之间的天数(EX: Start Date:29/OCT/2018 End Date: 07/NOV/2018),现在,它在提交时给出了天数(No. of days = 9),但是我想要这些天数当日期也增加时减少。请帮助给出一些想法

   <!DOCTYPE html>
<html>
 <head>
    <script type="text/javascript">
        function GetDays(){
                var dropdt = new Date(document.getElementById("end_date").value);
                var pickdt = new Date(document.getElementById("added_date").value);
                return parseInt((dropdt - pickdt) / (24 * 3600 * 1000));
        }

        function cal(){
        if(document.getElementById("end_date")){
            document.getElementById("calc").value=GetDays();
        }  
    }



    </script>
</head>
<body>

          <div class="form-row">

          <div class="form-group col-md-6">
                    <label>Invoice Date</label>
            <input type="text" class="form-control" id="added_date" name="added_date" value="<?php echo date("Y-m-d"); ?>" readonly/>
                    </div>

          </div>
          <div class="form-row">
            <div class="form-group col-md-6">
              <label>End Date</label>
    <input type="date" class="form-control" id="end_date" name="end_date" onchange="cal()" />
            </div>
            <div class="form-group col-md-6">
              <label>Warranty</label>
  <input type="text" class="form-control" id="calc" name="calc"value="" readonly/>
            </div>
          </div>






</body>

2 个答案:

答案 0 :(得分:0)

由于剩余保修是根据当前命运计算得出的,因此您需要通过当前日期,

var cuurent = new Date().getTime();

     return parseInt((dropdt - cuurent) / (24 * 3600 * 1000));

答案 1 :(得分:0)

请尝试以下操作并了解概念:

  var todaydate = new Date();
   var day = todaydate.getDate();
   var month = todaydate.getMonth() + 1;
   var year = todaydate.getFullYear();
   var datestring =month  + "/" +  day+ "/" + year;
   
   document.getElementById("added_date").value = datestring;
  
  function GetDays(){
  
  
   
                var dropdt = new Date(document.getElementById("end_date").value);
                var pickdt = new Date(document.getElementById("added_date").value);
                
                
                
                 var dateDifference = Math.floor((Date.UTC(dropdt.getFullYear(), dropdt.getMonth(), dropdt.getDate()) - Date.UTC(pickdt.getFullYear(), pickdt.getMonth(), pickdt.getDate())) / (1000 * 60 * 60 * 24));

                return dateDifference;
        }

        function cal(){
        if(document.getElementById("end_date")){
            document.getElementById("calc").value=GetDays();
        }  
        
        }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-row">

          <div class="form-group col-md-6">
                    <label>Invoice Date</label>
            <input type="text" class="form-control" id="added_date" name="added_date" readonly/>
                    </div>

          </div>
          <div class="form-row">
            <div class="form-group col-md-6">
              <label>End Date</label>
    <input type="date" class="form-control" id="end_date" name="end_date" onchange="cal()" />
            </div>
            <div class="form-group col-md-6">
              <label>Warranty</label>
  <input type="text" class="form-control" id="calc" name="calc"value="" readonly/>
            </div>
          </div>

并显示在表中,您需要在查询中使用date_diff(added_date,end_date)。