Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Below is my code how i calculate days result between two calendar . I have tried and it is work well. but the problem is it is just alert the number of day. Not appear as result or output after I select start date and end date from calendar . Can u help me how can I change that alert to be a result (or output appear in textbox or as label) automatically after user pick a date from calendar ? #please help me.

What I have tried:

HTML
<title>Travel Agency :: Home ::




 
$(document).ready(function(){
    var $datepicker1 =  $( "#datepicker1" );
    var $datepicker2 =  $( "#datepicker2" );
    $datepicker1.datepicker();
    $datepicker2.datepicker({
         onClose: function() {
            var fromDate = $datepicker1.datepicker('getDate');
            var toDate = $datepicker2.datepicker('getDate');
            // date difference in millisec
            var diff = new Date(toDate - fromDate);
            // date difference in days
            var days = diff/1000/60/60/24;

            alert(days);
        }
    });
});



<label>Start Date: <input type="text" id="datepicker1"></label>
<label>End Date: <input type="text" id="datepicker2"></label>

</body>
</html>
Posted
Updated 11-Dec-16 15:14pm
v4

1 solution

First, you have to reserve a place to display that "days", say a p tag with a id="noofday" after the last label tag, like this:
HTML
<p id="noofday"></p>

Next, change the "alert" statement to:
$("#noofday").html(days);

Refer to jQuery Tutorial[^] to learn.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900