简体   繁体   English

使用JavaScript计算两个日期之间的天数

[英]Calculate Days between two dates using javascript

I want this block of code to calculate the days between two dates. 我想要这段代码来计算两个日期之间的日期。 But it is not working. 但这是行不通的。 I tried a lot to find an error but failed. 我尝试了很多发现错误,但是失败了。 Can anyone please help. 谁能帮忙。 I went through the code but found no problem. 我检查了代码,但没有发现问题。 Still the calculator isn't working!!! 计算器仍然无法正常工作!!!

  <!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();" />&nbsp;
    </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>

Take out the hyphens in these two lines and change them to what I have here. 删除这两行中的连字符,然后将其更改为我在此处的内容。 It works perfectly now: 现在,它可以完美运行:

var firstDate = new Date(fromyear,frommonth,fromdate);
var secondDate = new Date(toyear,tomonth,todate);

Here is a link: http://dropoff.us/private/1364450059-1-answer1.html 这是链接: http : //dropoff.us/private/1364450059-1-answer1.html

Update: corrected link with button removed and onchange handler added to date picker: http://dropoff.us/private/1364450310-1-answer2.html 更新:纠正了链接,其中删除了按钮,并在日期选择器中添加了onchange处理程序: http : //dropoff.us/private/1364450310-1-answer2.html

since you are using jquery datepicker , you could do: 由于您正在使用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);
   }
}); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM