0
$(function () {
    $("#from").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 3,
        onClose: function (selectedDate) {
            $("#to").datepicker("option", "minDate", selectedDate);
        }
    });
    $("#to").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 3,
        onClose: function (selectedDate) {
            $("#from").datepicker("option", "maxDate", selectedDate);
        }
    });
});

$("#to").change(function () {
    var final = $("#to").datepicker("getDate");
    var start = $("#from").datepicker("getDate");
    var days = new Date(final - start);
    alert(days);
});

<label for="from">From</label>
<input type="text" id="from" name="from" />
<label for="to">to</label>
<input type="text" id="to" name="to" />

This is what I have by far, I pretend to get the number of days passed between 2 dates everytime that final date is filled. I'm having some trouble because of conversions :s

Fiddle

2 Answers 2

5

I believe THe biggest thing is that datepicker('getDate') returns a Date object already, so final and start are already date objects.

Now that you have two date objects you could the code samples in the following thread to get the number of days between them

How to calculate the number of days between two dates using JavaScript?

The following code is ripped right from the thread and google: you can fill in appropriately

var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var firstDate = new Date(2008,01,12);
var secondDate = new Date(2008,01,22);

var diffDays = Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay));
2
  • Yes it is working. I'm just wondering if it is the rigth way to do it. Just like if it is a trick or it is a real solution Commented Apr 11, 2013 at 14:53
  • @user1148875 idk, google around, if your wondering if there is some sort of DateDifferential or some sort of standard language way to get the days between two dates, there doesn't look to be any. Stackoverflow is full of answers on how to get time between two dates, there looks to be quite a few ways, i just referred you to the first one i found on so
    – dm03514
    Commented Apr 11, 2013 at 14:57
0

The following example worked fine for me.

$(document).ready(function(){
   var $datepicker1 =  $( "#firstday" );
   var $datepicker2 =  $( "#lastday" );
   $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);
       }
   });
});

Refer:- calculate number of days between two dates in jquery datepicker

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.