Start Building Professional
Web Apps Today


 
Categories Question details Back To List
Question  posted by David Keenan on Dec 10, 2008 05:58
open dhtmlx forum
Count number of days between two dates in the double calendar

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
Answer posted by Support on Dec 10, 2008 06:37
dblc.setOnClickHandler(function(){
   var time = dblc.rightCalendar.getDate() - dblc.leftCalendar.getDate(); //get difference
   document.getElementById("some_form").value = Math.floor(time / (1000*60*60*24)); //convert it  to days
   return true;
});
Answer posted by David Keenan on Dec 10, 2008 07:10

 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);
  })

Answer posted by Support on Dec 10, 2008 07:38
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 / (1000*60*60*24)); //convert it to days
  return true;
  })

Answer posted by David Keenan on Dec 10, 2008 07:57

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 / (1000*60*60*24)); //convert it to days
  return true;
  }) 

}

Answer posted by David Keenan on Dec 10, 2008 08:57

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 / (1000*60*60*24)); //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*60*60*24)); //convert it to days
  return true;
  
 }

Answer posted by Support on Dec 10, 2008 09:58
Please check attached sample. 
Attachments (1)
Answer posted by David Keenan on Dec 10, 2008 12:14

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

Answer posted by Support on Dec 11, 2008 04:56
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. 
Answer posted by David Keenan on Dec 11, 2008 06:40

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??

 

Answer posted by David Keenan on Dec 11, 2008 09:32

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/(1000*60*60*24)));
   },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*60*60*24)); //convert it to days
  return true;
  
 }