Thursday, 23 January 2014

Calculate the days difference between two dates using Jquery

$(document).ready(function () {
$("#date1").datepicker({ minDate: new Date(2012, 7 - 1, 8), maxDate: new Date(2012, 7 - 1, 28) });
//$("#date2").datepicker({ minDate: new Date(2012, 7 - 1, 9), maxDate: //new Date(2012, 7 - 1, 28) });
 
$('#date1, #date2').datepicker({onSelect: function(dateStr) {
      var d1 = $('#date1').datepicker('getDate');
      var d2 = $('#date2').datepicker('getDate');
      var diff = 0;
      if (d1 && d2) {
            diff = Math.floor((d2.getTime() - d1.getTime()) / 86400000); // ms per day
      }
      $('#calculated').val(diff);
}
});
});

Check Js Fiddle : http://jsfiddle.net/MebwN/48/

No comments:

Post a Comment