Javascript - 两个日期之间的差异

Javascript - Difference between two dates

我想计算在表单页面的两个字段中写入的两个日期之间的差异。 html 代码下方:

<!-- CALENDARIO -->
<script src="js/datetimepicker_css.js"></script>
<!-- CALENDARIO -->

Altro codice html


<tr>
    <td width="306" valign="top">
<div class="col-xs-8">
    Data inizio<br>
    <div class="input-group">
      <input type="text" class="form-control" name="data_inizio" id="date_validation1" maxlength="25" size="20"/>
      <span class="input-group-addon">
        <span class="glyphicon glyphicon-calendar" onclick="javascript:NewCssCal ('date_validation1','yyyyMMdd')"  style="cursor:pointer"></span>
      </span>
    </div>
</div>                        
    </td>
    <td width="307" valign="top">
<div class="col-xs-8">
    Data fine<br>
    <div class="input-group">
      <input type="text" class="form-control" name="data_fine" id="date_validation2" maxlength="25" size="20"/>
      <span class="input-group-addon">
        <span class="glyphicon glyphicon-calendar" onclick="javascript:NewCssCal ('date_validation2','yyyyMMdd')"  style="cursor:pointer"></span>
      </span>
    </div>
</div>    
    </td>
    <td width="307" valign="top">
    <div class="col-xs-8">
        Numro giorni<br>
        <input type="text" class="form-control" name="numero_giorni"></div>
    </td>
</tr>

如何计算两个日期之间的差异并将差异写在字段名称中="days"?

谢谢

计算两个日期之间的差异并将该值分配给另一个文本框

<input type="text" id="val1"/>
<input type="text" id="val2"/>
<input type="text" id="val3" />
<script>


function myFun() {

var x2=document.getElementById("val1").value;
var x3=document.getElementById("val2").value;
var msPerDay = 1000*60*60*24;
d1=new Date(x2);
d2=new Date(x3);
var x4=document.getElementById("val3");
var dd=( ((d1 - d2) / msPerDay).toFixed(0)+"days" );
    x4.value=dd;
    }

</script>
<input type="submit" id="myid" onclick="myFun()" value="get difference"/>