计算两天之间的天数

时间:2017-02-22 06:59:10

标签: jquery

我正在尝试计算两个选定日期的天数,我的代码无效。是否有任何错误?

  				    
   <label>Date From</label>
   <input type="date" name="levFrom" id="levFrom" class="levFrom">
				   
   <label >Date To</label>
   <input type="date" name="levTo"  id="levTo" class="levTo">
			
   <label >Duration</label>
   <input type="text" name="levDuration" id="levDuration" class="levDuration" readonly="readonly">
				
PowerMockito.mockStatic(SomeClass.class)

3 个答案:

答案 0 :(得分:2)

您的calulateDays功能缺失}。此外,js区分大小写。因此,当您访问它们时,您必须使用levfromlevto小写ft

$(document).ready(function(){
        calulateDays()
});

function calulateDays(){
    $('#levTo').on('keyup mouseup change',function(){
    var levfrom=$('#levFrom').val();
        levto=$('#levTo').val();
        from,to,duration;

    from=moment(levFrom, 'YYYY-MM-DD');   // HERE: levfrom
    to=moment(levTo,'YYYY-MM-DD');        // HERE: levto
    duration=to.diff(from,'days')    
    $('#levDuration').val(duration + ' days')

  });
}  // <==== HERE

另外,请考虑改进您的js代码:

  $(document).ready(calulateDays);

  function calulateDays() {
    $('#levTo').on('keyup mouseup change', function () {
      var levfrom = $('#levFrom').val(),
        levto = $('#levTo').val(),
        from, to, duration;

      from = moment(levfrom, 'YYYY-MM-DD');
      to = moment(levto, 'YYYY-MM-DD');
      duration = to.diff(from, 'days')
      $('#levDuration').val(duration + ' days')
    });
  }

答案 1 :(得分:2)

试试这个兄弟

&#13;
&#13;
$(document).ready(function(){
            calulateDays()
        
    });

    function calulateDays(){
        //$('#levTo').on('keyup mouseup change',function(){
        $('#levTo').on('change',function(){
        var levfrom=$('#levFrom').val();
        var levto=$('#levTo').val();
        var from,to,duration;
    
        from=moment(levfrom, 'YYYY-MM-DD');
        to=moment(levto,'YYYY-MM-DD');
        duration=to.diff(from,'days');
        $('#levDuration').val(duration + ' days')
    
    });};
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.1/moment.min.js"></script>
<label>Date From</label>
   <input type="date" name="levFrom" id="levFrom" class="levFrom">
				   
   <label >Date To</label>
   <input type="date" name="levTo"  id="levTo" class="levTo">
			
   <label >Duration</label>
   <input type="text" name="levDuration" id="levDuration" class="levDuration" readonly="readonly">
&#13;
&#13;
&#13;

答案 2 :(得分:1)

    function calulateDays(){
        var levfromArr=$('#levFrom').val().split('-'),               levtoArr=$('#levTo').val().split('-');
        var leavefromDay=levfromArr.pop();
        var levfromMonth=levfromArr.pop();
        var levfromYear=levfromArr.pop();
        
        var levtoDay=levtoArr.pop();
        var levtoMonth=levtoArr.pop();
        var levtoYear=levtoArr.pop();
        
        var oneDay = 24*60*60*1000; 
        var firstDate = new Date(levfromYear,levfromMonth,leavefromDay);
        var secondDate = new Date(levtoYear,levtoMonth,levtoDay);

        var diffDays = Math.round(Math.abs((firstDate.getTime() -           secondDate.getTime())/(oneDay)));
         
        $('#levDuration').val(diffDays + ' days')
    
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<label>Date From</label>
   <input type="date" name="levFrom" id="levFrom" class="levFrom">
				   
   <label >Date To</label>
   <input type="date" name="levTo"  id="levTo" class="levTo">
			
   <button onclick="calulateDays()"  >Duration</button>
   <input type="text" name="levDuration" id="levDuration" class="levDuration" readonly="readonly">

试一试。它的工作片段