Getting datepicker to count number of days and show the days in between

Getting datepicker to count number of days and show the days in between

Hi,

Thank you for developing jQuery UI datepicker. It is so user-friendly and beautiful, we don't have to code a calendar all from scratch! 

I am using the datepicker to show 2 calendars and it is working great! But I couldn't get the number of days to show and I couldn't get it to list out the days in between... Please help me! It might be some stupid mistake I made hahah.

For a start I couldn't even get it to count number of days...

My code:

  1.   <script>
  2.     $(document).ready(function() {
  3.     $( "#from" ).datepicker({
  4.  altField: "#alternate",
  5.       altFormat: "DD, d MM, yy",
  6.     //minimal date to select (today)
  7. minDate: new Date( (new Date()).getTime() ),
  8.    maxDate: "+60D",
  9. onSelect: function(selected) {
  10.           $("#to").datepicker("option","minDate", selected)
  11. // alert(date1);
  12. }
  13.   });

  14.     $( "#to" ).datepicker({
  15.       altField: "#alternate1",
  16.       altFormat: "DD, d MM, yy",
  17.    minDate: new Date( (new Date()).getTime() + 3600*24*1000 ),
  18.         maxDate:"+60D",
  19.         onSelect: function(selected) {
  20.            $("#from").datepicker("option","maxDate", selected)
  21. }
  22.     });

  23.    function calcDiff() {
  24. var date1 = $(this).datepicker('getDate');
  25.         document.getElementById("alternate").value = date1;
  26. var date2 = $(this).datepicker('getDate');
  27.         document.getElementById("alternate1").value = date2;
  28.             var diff = 0;
  29.             if (date1 && date2) {
  30.                   diff = Math.floor((date2.getTime() - date1.getTime()) / 86400000); // ms per day
  31.             }
  32.             $('#calculated').val(diff);
  33. }
  34. });

  35.   </script>
My html for calculated is:

  1. <input type="text" id="calculated">
The 2 calendars are working so I won't show it here...

Is there something wrong with the code? I have coded it according to one of the reply here in the forum.


Please help! Thanks!