我希望这段代码计算两个日期之间的天数。但它没有用。我尝试了很多找错误但失败了。谁能请帮忙。我仔细检查了代码,但没有发现任何问题。计算器还没有工作!!!
<!DOCTYPE html>
<html><head>
<meta charset="utf-8" />
<title>jQuery UI Datepicker - Default functionality</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<style>
.ui-datepicker {font-size:11px;}
</style>
<script>
$(function() {
$( "#from" ).datepicker();
});
$(function() {
$( "#to" ).datepicker();
});
</script>
</head><body>
<form class="formwhite">
<table><tr><td>From:</td><td><input type="text" id="from" onKeyUp="calculate();" />
</td></tr>
<tr><td>To:</td><td><input type="text" id="to" onKeyUp="calculate();" /></td></tr>
</table>
</form>
<span id="result"></span>
<script>
var calculate = function() {
var from = document.getElementById("from").value;
var fromdate = from.slice(3, 5);
fromdate = parseInt(fromdate);
var frommonth = from.slice(0, 2);
frommonth = parseInt(frommonth);
var fromyear = from.slice(6, 10);
fromyear = parseInt(fromyear);
var to = document.getElementById("to").value;
var todate = to.slice(3, 5);
todate = parseInt(todate);
var tomonth = to.slice(0, 2);
tomonth = parseInt(tomonth);
var toyear = to.slice(6, 10);
toyear = parseInt(toyear);
var oneDay = 24*60*60*1000;
var firstDate = new Date(fromyear,frommonth,fromdate);
var secondDate = new Date(toyear,tomonth,todate);
var diffDays = Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay)));
document.getElementById("result").innerHTML=diffDays;
}
</script></body></html>
答案 0 :(得分:1)
取出这两行中的连字符,然后将它们更改为我在这里的内容。它现在完美地运作:
var firstDate = new Date(fromyear,frommonth,fromdate);
var secondDate = new Date(toyear,tomonth,todate);
这是一个链接:http://dropoff.us/private/1364450059-1-answer1.html
更新:已删除按钮的更正链接以及添加到日期选择器的onchange处理程序:http://dropoff.us/private/1364450310-1-answer2.html
答案 1 :(得分:0)
因为你正在使用jquery datepicker,你可以这样做:
$('#to').datepicker({
onSelect: function(dateText, inst) {
var d1=new Date(dateText);
var d2=new Date($('#from').val());
var diff = (Math.abs((d2-d1)/86400000)); //days between 2 dates
$("#result").html(diff);
}
});