We help IT Professionals succeed at work.

Adding/substracting  form fields to calculate "number of nights"

planeta
planeta asked
on
I have a form with the following structure:
Arrival: (day)/(month)/(year)
Departure:(day)/(month)/(year)

and I want to calculate and show the "number of nights", substracting the departure - arrival from the above form fields:
Help? thx!
Comment
Watch Question

Commented:
Can't help you myself but I bets ya there is an answer to you question at the following URL

http://www.developer.irt.org/script/date.htm

Hope helps

eilLOC
CERTIFIED EXPERT
Top Expert 2005

Commented:
Here is a function to give you the difference between two hard-coded dates. I am going generalize it for you in a few minutes:

<SCRIPT LANGUAGE=javascript>
<!--
function DateDifference()
{


date1= Date.parse("2001/10/01")
date2= Date.parse("2001/10/03")
iDifference = (date2-date1)/(24*60*60*1000)
alert(iDifference)

}
//-->
</SCRIPT>
CERTIFIED EXPERT
Top Expert 2005

Commented:
Here it is in a general form:

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
function DateDifference()
{
arrivalDate = document.form1.aYear.value + "/" + document.form1.aMonth.value + "/" +document.form1.aDay.value
DepartureDate = document.form1.dYear.value + "/" + document.form1.dMonth.value + "/" +document.form1.dDay.value

date1= Date.parse(arrivalDate)
date2= Date.parse(DepartureDate)
iDifference = (date2-date1)/(24*60*60*1000)
alert(iDifference)
}
//-->
</SCRIPT>


</HEAD>
<BODY>
<FORM action="" method=POST id=form1 name=form1>
Arrival:<br>
Year:<INPUT type="text"  name=aYear><br>
Month:<INPUT type="text"  name=aMonth><br>
Day:<INPUT type="text"  name=aDay><br>
<p>
<p>
Departure:<br>
Year:<INPUT type="text"  name=dYear><br>
Month:<INPUT type="text"  name=dMonth><br>
Day:<INPUT type="text"  name=dDay><br>
<INPUT type="button" value="Show Difference" id=button1 name=button1 onClick ="JavaScript:DateDifference()">
<p>
</FORM>
</BODY >
</HTML>

Commented:
I wrote a function to calculate the difference between two dates based on the interval you passed it.  It does assume the date come in mm/dd/yyyy format.  The intervals you can pass it are shown in the switch statement.  It discards any fractions of the interval:

//gives difference between two dates in the specified interval
//returns positive value if date2 > date1
function dateDiff(interval, date1, date2)
{
     
     date1 = fourDigitYear(date1);
     date2 = fourDigitYear(date2);
     var d1 = new Date(date1);
     var d2 = new Date(date2);
     
     var diff = Date.parse(d2) - Date.parse(d1);

     switch (interval)
     {
          case "s":     //seconds
               diff = Math.floor(diff/1000);
               break;
          case "n":     //minutes
               diff = Math.floor(diff/60000);
               break;
          case "h":     //hours
               diff = Math.floor(diff/3600000);
               break;
          case "d":     //days
               diff = Math.floor(diff/86400000);
               break;
          case "w":     //weeks
               diff = Math.floor(diff/604800000);
               break;
          case "m":     //months
               diff = Math.floor(diff/2678400000);
               break;
          case "y":     //years
               diff = Math.floor(diff/31536000000);
               break;
     }
     
     return(diff);
     
}

Author

Commented:
FritZ the BLank:
just a little more help:
i need to show the difference in a text box in the same form, not in a pop up!

Author

Commented:
FritZ the BLank:
just a little more help:
i need to show the difference in a text box in the same form, not in a pop up!
CERTIFIED EXPERT
Top Expert 2005

Commented:
Sorry about that! Here you go:

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
function DateDifference()
{
arrivalDate = document.form1.aYear.value + "/" + document.form1.aMonth.value + "/" +document.form1.aDay.value
DepartureDate = document.form1.dYear.value + "/" + document.form1.dMonth.value + "/" +document.form1.dDay.value

date1= Date.parse(arrivalDate)
date2= Date.parse(DepartureDate)
document.form1.DaysDifference.value = (date2-date1)/(24*60*60*1000)

}
//-->
</SCRIPT>


</HEAD>
<BODY>
<FORM action="" method=POST id=form1 name=form1>
Arrival:<br>
Year:<INPUT type="text"  name=aYear><br>
Month:<INPUT type="text"  name=aMonth><br>
Day:<INPUT type="text"  name=aDay><br>
<p>
<p>
Departure:<br>
Year:<INPUT type="text"  name=dYear><br>
Month:<INPUT type="text"  name=dMonth><br>
Day:<INPUT type="text"  name=dDay><br>

<hr>
Number of Days:<br>
<INPUT type="text"  name=DaysDifference><br>
<hr>
<INPUT type="button" value="Show Difference" id=button1 name=button1 onClick ="JavaScript:DateDifference()">
<p>
</FORM>
</BODY >
</HTML>
CERTIFIED EXPERT
Top Expert 2005

Commented:
The above should work fine. If I were you, however, you would want to make this code a little more robust by adding JavaSCript validation to ensure that users can enter only appropriate data in each of the fields, i.e., only numbers not letters, month <13, days <32 and etc.

Fritz the Blank
CERTIFIED EXPERT
Top Expert 2005
Commented:
This version ensures that valid dates are entered:

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<SCRIPT LANGUAGE=javascript>
<!--
function checkDate(strDate)
{
     if(strDate.length>0)
          {
             var dateregex=/^[ ]*[0]?(\d{1,2})\/(\d{1,2})\/(\d{4,})[ ]*$/;
              var match=strDate.match(dateregex);
              if (match)
               {
                        var tmpdate=new Date(match[3],parseInt(match[1])-1,match[2]);
                   if (tmpdate.getDate()==parseInt(match[2]) && tmpdate.getFullYear()==parseInt(match[3]) && (tmpdate.getMonth()+1)==parseInt(match[1]))
                    {
                    return true;
                    }
              }
          alert("Please enter valid values for the day, month and year!");
          return false;
          }
     else
          {
          return true;
          }
}


function DateDifference()
{

arrivalDate = document.form1.aMonth.value + "/" + document.form1.aDay.value + "/" + document.form1.aYear.value
DepartureDate = document.form1.dMonth.value + "/" + document.form1.dDay.value + "/" +document.form1.dYear.value

if(checkDate(arrivalDate) && checkDate(DepartureDate))
     {
     arrivalDate = document.form1.aYear.value + "/" + document.form1.aMonth.value + "/" + document.form1.aDay.value
     DepartureDate = document.form1.dYear.value + "/" + document.form1.dMonth.value + "/" + document.form1.dDay.value
     date1= Date.parse(arrivalDate)
     date2= Date.parse(DepartureDate)
     document.form1.DaysDifference.value = (date2-date1)/(24*60*60*1000)
     }
}
//-->
</SCRIPT>


</HEAD>
<BODY>
<FORM action="" method=POST id=form1 name=form1>
Arrival:<br>
Year:<INPUT type="text"  name=aYear><br>
Month:<INPUT type="text"  name=aMonth><br>
Day:<INPUT type="text"  name=aDay><br>
<p>
<p>
Departure:<br>
Year:<INPUT type="text"  name=dYear><br>
Month:<INPUT type="text"  name=dMonth><br>
Day:<INPUT type="text"  name=dDay><br>

<hr>
Number of Days:<br>
<INPUT type="text"  name=DaysDifference><br>
<hr>
<INPUT type="button" value="Show Difference" id=button1 name=button1 onClick ="JavaScript:DateDifference()">
<p>
</FORM>
</BODY >
</HTML>

Author

Commented:
You're GREAT!!