Javascript - 两个日期之间的差异

时间:2015-06-26 23:15:44

标签: javascript jquery datepicker calendar

我想计算表格页面两个字段中写的两个日期之间的差异。在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>

如何计算两个日期之间的差异,并在字段名称=“天”中写下差异?

由于

1 个答案:

答案 0 :(得分:0)

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

<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"/>