JS日期相减
<input type="text" id="ipt1" value="2008-10-21" onblur="cal()" /> <input type="text" id="ipt2" value="2008-11-12" onblur="cal()" />
<input type="text" id="ipt3" value="" />
<script language="JavaScript">
<!--
String.prototype.trim = function() {
return this.replace(/(^[\uFEFF|\u00A0|\s]*)|([\uFEFF|\u00A0|\s]*$)/g, "");
};
function cal() {
var ipt1 = document.getElementById("ipt1").value;
var ipt2 = document.getElementById("ipt2").value;
if (ipt1.trim() == "" || ipt2.trim() == "") {
return;
}
var arr1 = ipt1.split("-");
var arr2 = ipt2.split("-");
var dt1 = new Date();
dt1.setFullYear(arr1[0]);
dt1.setMonth(arr1[1] - 1);
dt1.setDate(arr1[2]);
var dt2 = new Date();
dt2.setFullYear(arr2[0]);
dt2.setMonth(arr2[1] - 1);
dt2.setDate(arr2[2]);
var dif = dt2.getTime() - dt1.getTime();
var days = dif / (24 * 60 * 60 * 1000);
document.getElementById("ipt3").value = days;
}
//-->
</script>
这个代码需要点击两个框才可以算出天数 ,请问JS直接相减天数怎么写