user2197789
user2197789

Reputation: 103

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>

Upvotes: 2

Views: 9554

Answers (2)

Joseph Myers
Joseph Myers

Reputation: 6552

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

Update: corrected link with button removed and onchange handler added to date picker: http://dropoff.us/private/1364450310-1-answer2.html

Upvotes: 1

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100195

since you are using jquery datepicker, you could do:

$('#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);
   }
}); 

Upvotes: 0

Related Questions