Count number of days between two dates in the double calenda

How can I output (to a form) the number of days between the two dates in the double calandar?



And each time this changes the count should change too?



Thanks for the help



David

dblc.setOnClickHandler(function(){
var time = dblc.rightCalendar.getDate() - dblc.leftCalendar.getDate(); //get difference
document.getElementById(“some_form”).value = Math.floor(time / (10006060*24)); //convert it to days
return true;
});


 Thanks, This looks great but how do I use it with the OnClickHandler below as well?



mDCal.setOnClickHandler(function(date,self,type){
   if (type==“right”)
    document.getElementById(“return”).value = mDCal.rightCalendar.getFormatedDate(null,date);
   else
    document.getElementById(“depart”).value = mDCal.leftCalendar.getFormatedDate(null,date);
  })

Just combine code of both methods

mDCal.setOnClickHandler(function(date,self,type){
//set input value
if (type==“right”)
document.getElementById(“return”).value = mDCal.rightCalendar.getFormatedDate(null,date);
else
document.getElementById(“depart”).value = mDCal.leftCalendar.getFormatedDate(null,date);
//set date difference
var time = dblc.rightCalendar.getDate() - dblc.leftCalendar.getDate(); //get difference
document.getElementById(“some_form”).value = Math.floor(time / (10006060*24)); //convert it to days
return true;
})


Yeah, I tried that but keep getting NaN in the input text box??? Not sure if the mods I made to your script are correct, but it looks right??



 window.onload = function () {
 
  var StartBookingDate=new Date();
 var NextBookingDate=new Date();
      NextBookingDate.setDate(NextBookingDate.getDate()+5);
 var PrevBookingDate=new Date();
      PrevBookingDate.setDate(PrevBookingDate.getDate()-1);
  var EndBookingDate=new Date();
       EndBookingDate.setDate(StartBookingDate.getDate()+28);
 
  var StartBookingYear=StartBookingDate.getYear();
  var EndBookingYear=(EndBookingDate.getYear()+1);
 
 
  mDCal = new dhtmlxDblCalendarObject(‘dhtmlxDblCalendar’, false, {isMonthEditable: true, isYearEditable: true});
  mDCal.setYearsRange(StartBookingYear,EndBookingYear);
  mDCal.setDateFormat("%d/%m/%Y");
  mDCal.leftCalendar.setSensitive(new Date(StartBookingDate),null)
  mDCal.rightCalendar.setSensitive(new Date(StartBookingDate),null)
  mDCal.leftCalendar.setInsensitiveDates(“12-25,12-26,12-27,12-28”);
  mDCal.rightCalendar.setInsensitiveDates(“12-25,12-26,12-27,12-28”); 
   mDCal.setDate(document.getElementById(‘depart’).value,document.getElementById(‘return’).value);
  mDCal.draw();
  mDCal.setOnClickHandler(function(date,self,type){
  if (type==“right”)
  document.getElementById(“return”).value = mDCal.rightCalendar.getFormatedDate(null,date);
  else
  document.getElementById(“depart”).value = mDCal.leftCalendar.getFormatedDate(null,date);
  var time = mDCal.rightCalendar.getDate() - mDCal.leftCalendar.getDate(); //get difference
  document.getElementById(“daycount”).value = Math.floor(time / (10006060*24)); //convert it to days
  return true;
  }) 



}


Its getting there now… but you have to double click it to get the correct number, can you have a look at this??



!!! Actually just worked out it is lagging, each time you click it shows the previous number??



Thanks



 window.onload = function () {
 
  var StartBookingDate=new Date();
 var NextBookingDate=new Date();
      NextBookingDate.setDate(NextBookingDate.getDate()+5);
 var PrevBookingDate=new Date();
      PrevBookingDate.setDate(PrevBookingDate.getDate()-1);
  var EndBookingDate=new Date();
       EndBookingDate.setDate(StartBookingDate.getDate()+28);
 
  var StartBookingYear=StartBookingDate.getYear();
  var EndBookingYear=(EndBookingDate.getYear()+1);
 
 
  mDCal = new dhtmlxDblCalendarObject(‘dhtmlxDblCalendar’, false, {isMonthEditable: true, isYearEditable: true});
  mDCal.setYearsRange(StartBookingYear,EndBookingYear);
  mDCal.setDateFormat("%d/%m/%Y");
  mDCal.leftCalendar.setSensitive(new Date(StartBookingDate),null)
  mDCal.rightCalendar.setSensitive(new Date(StartBookingDate),null)
  mDCal.leftCalendar.setInsensitiveDates(“12-25,12-26,12-27,12-28”);
  mDCal.rightCalendar.setInsensitiveDates(“12-25,12-26,12-27,12-28”);
   mDCal.setHolidays([‘12-25’,‘12-26’,‘12-27’,‘12-28’]);
   mDCal.setDate(document.getElementById(‘depart’).value,document.getElementById(‘return’).value);
  mDCal.draw();
  mDCal.setOnClickHandler(function(date,self,type){
//set input value
  if (type==“right”)
  document.getElementById(“return”).value = mDCal.rightCalendar.getFormatedDate(null,date);
  else
  document.getElementById(“depart”).value = mDCal.leftCalendar.getFormatedDate(null,date);
//set date difference
  var time2 = Date.parse(mDCal.rightCalendar.getDate()) - Date.parse(mDCal.leftCalendar.getDate()); //get difference
  document.getElementById(“daycount”).value = Math.floor(time2 / (1000606024)); //convert it to days
  return true;
  })
 
// set date difference at page load
  var time1 = Date.parse(mDCal.rightCalendar.getDate()) - Date.parse(mDCal.leftCalendar.getDate()); //get difference
  document.getElementById(“daycount”).value = Math.floor(time1 / (1000
606024)); //convert it to days
  return true;
  
 }

Please check attached sample.
1228931843.zip (22.7 KB)


Yeah, really cool way to show it and will probably use that somewhere but need to pass the value to the next page in a form, the same way I have the dates in the input boxes.



Will keep trying to get it working.



Thanks Again,



David

The sample above shows that provided logic can be used for date difference calculation, so you just need to store the same value in form’s input.


Great Answer and sound like you are on the right track but the question is how??, as I spent a few hours trying to work out how to get it from the alert to the form but keep getting the lag whatever I do??



Left scrathing my head as it should work and cant understand why the lag occurs??



 


GOT IT, Thanks to your code, working perfect, thanks again and here is the finish product!!! (well that part anyway!!)



 window.onload = function () {
 
  var StartBookingDate=new Date();
 var NextBookingDate=new Date();
      NextBookingDate.setDate(NextBookingDate.getDate()+5);
 var PrevBookingDate=new Date();
      PrevBookingDate.setDate(PrevBookingDate.getDate()-1);
  var EndBookingDate=new Date();
       EndBookingDate.setDate(StartBookingDate.getDate()+28);
 
  var StartBookingYear=StartBookingDate.getYear();
  var EndBookingYear=(EndBookingDate.getYear()+1);
 
 
  mDCal = new dhtmlxDblCalendarObject(‘dhtmlxDblCalendar’, false, {isMonthEditable: true, isYearEditable: true});
  mDCal.setYearsRange(StartBookingYear,EndBookingYear);
  mDCal.setDateFormat("%d/%m/%Y");
  mDCal.leftCalendar.setSensitive(new Date(StartBookingDate),null)
  mDCal.rightCalendar.setSensitive(new Date(StartBookingDate),null)
  mDCal.leftCalendar.setInsensitiveDates(“12-25,12-26,12-27,12-28”);
  mDCal.rightCalendar.setInsensitiveDates(“12-25,12-26,12-27,12-28”);
   mDCal.setHolidays([‘12-25’,‘12-26’,‘12-27’,‘12-28’]);
   mDCal.setDate(document.getElementById(‘depart’).value,document.getElementById(‘return’).value);
  mDCal.draw();
  mDCal.setOnClickHandler(function(date,self,type){
//set input value
  if (type==“right”)
  document.getElementById(“return”).value = mDCal.rightCalendar.getFormatedDate(null,date);
  else
  document.getElementById(“depart”).value = mDCal.leftCalendar.getFormatedDate(null,date);
//set date difference
     window.setTimeout(function(){
     var time2 = Date.parse(mDCal.rightCalendar.getDate()) - Date.parse(mDCal.leftCalendar.getDate()); //get difference
     document.getElementById(‘daycount’).value=(Math.floor(time2/(1000606024)));
   },1)
   })
 
// set date difference at page load
  var time1 = Date.parse(mDCal.rightCalendar.getDate()) - Date.parse(mDCal.leftCalendar.getDate()); //get difference
  document.getElementById(“daycount”).value = Math.floor(time1 / (1000
606024)); //convert it to days
  return true;
  
 }