PDA

View Full Version : Bizarre problem with date difference script

sunkiss1
12-15-2006, 02:30 PM
Hi to all,
I have a rather long and complex script that controls the reservations for our vacation homes. Part of the script determines the length of stay by determining the difference between the arrival date and departure date. You can see the reservation form in action at:

http://www.sunkissvillas.com/reservations/disneyluxury/reservations.htm

The entire script works well but I recently discovered that when someone enters an arrival date at the end of March, 2007 and a departure date in April of the next month the number of nights (length of stay) is short by one night. All other months work properly, inlcuding the following year for the same months.

Here is a snippet of the code that determines the length of stay:

<!--Start
//The following sections determines the number of days between the curretn date and the arrival date
function duedateDiff(charges) {
date1 = new Date();
date2 = new Date();
diff = new Date();

if (isValidDate(charges.currentdate.value) && isValidTime(charges.firsttime.value)) { // Validates first date
date1temp = new Date(charges.currentdate.value + " " + charges.firsttime.value);
date1.setTime(date1temp.getTime());
}
else return false; // otherwise exits

if (isValidDate(charges.date8a.value) && isValidTime(charges.secondtime.value)) { // Validates second date
date2temp = new Date(charges.date8a.value + " " + charges.secondtime.value);
date2.setTime(date2temp.getTime());
}
else return false; // otherwise exits

// sets difference date to difference of first date and second date

diff.setTime(Math.abs(date1.getTime() - date2.getTime()));

timediff = diff.getTime();

weeks = Math.floor(timediff / (1000 * 60 * 60 * 24 * 7));
timediff -= weeks * (1000 * 60 * 60 * 24 * 7);

days = Math.floor(timediff / (1000 * 60 * 60 * 24));
timediff -= days * (1000 * 60 * 60 * 24);

hours = Math.floor(timediff / (1000 * 60 * 60));
timediff -= hours * (1000 * 60 * 60);

mins = Math.floor(timediff / (1000 * 60));
timediff -= mins * (1000 * 60);

secs = Math.floor(timediff / 1000);
timediff -= secs * 1000;

charges.duedatedif.value = (weeks * 7) + days ;

return false; // form should never submit, returns false
}
<!--End

You can find the entire code here:
http://www.sunkissvillas.com/JS/FormCalcs6bed.js

This one has me stumped. :confused: Any help or advise is appreciated.
Thanks,
Mark

Ancora
12-15-2006, 03:25 PM
Mark, take a look at this:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<title>Days Apart</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">

var today = new Date();
today.setHours(0,0,0,0);

function validate(isField){

var splitDate = isField.value.split("/");
var refDate = new Date(isField.value);
if (splitDate[0] < 1 || splitDate[0] > 12 || refDate.getDate() != splitDate[1] || splitDate[2].length != 4 || (!/^20/.test(splitDate[2])))
{
isField.value = "";
isField.focus();
return false;
}
isField.value = isField.value.replace(/^(\d{1}\/)/,"0\$1").replace(/(\d{2}\/)(\d{1}\/)/,"\$10\$2")
return refDate;
}

function verify(nForm){

var startDate = validate(nForm['fromDate']);
var endDate = validate(nForm['toDate']);
if (startDate && endDate)
{
if (startDate > endDate){alert('Invalid From Date'); return false}
if (startDate < today){alert('Invalid From Date'); return false}
var daysApart = Math.round((endDate-startDate)/86400000);
nForm['daysApart'].value = daysApart;
return true;
}
return false;
}

</script>
<style type="text/css">

body {background-color:#eae3c6;margin-top:60px}
form {width:250px;margin:auto}
fieldset {width:250px;background-color:#f0fff0;border:1px solid #87ceeb}
.submitBtn {font-family:tahoma;font-size:10pt;display:block;margin-left:auto;margin-right:auto;margin-top:5px;margin-bottom:5px}

</style>
<body>
<form action="" onsubmit="return verify(this)">
<fieldset>
<legend>Days Apart</legend>
<label>From: (mm/dd/yyyy): <input type='text' size='9' name='fromDate'></label>
<label>To: (mm/dd/yyyy):&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='text' size='9' name='toDate'></label>
<label>Days Apart:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='text' size='3' name='daysApart' readonly></label>
<input type='submit' name='submit' value="Submit" class='submitBtn'>
</fieldset>
</form>
</body>
</html>

sunkiss1
12-15-2006, 04:01 PM
Hi Mike,
Thanks for the script. Have you had any problems with it? I tried it a little bit and it came out with the correct numbers. I'll try incorporating it into my reservation system.

Do you have any ideas as to what was happening in the script I posted? I would be interested to know if you have some insites.
Thanks a bunch,
Mark :)

Ancora
12-15-2006, 04:14 PM
Hi Mark:

No, I haven't had any problems with my script. I tested in again before I posted it here, just to be sure.

I'm sorry, but I don't like to leave the forum to look at script. I looked at the code you posted and it goes through a lot of calculations to compute days apart, when that really isn't necessay. It's done with just one line, in my code. So, there was no need for me to try and test your code.

I don't think you'll have any trouble incorporating my code in to your form, but let me know if you do.

sunkiss1
12-23-2006, 06:50 PM
Hi Mike,
Again thanks for the script but I'm having a problem modifying it for my needs. I need the script to insert the days apart into the "daysapart" field when focus is given to that field instead of on submit. I've tried several things but I'm not seeing something (not getting it). Thanks in advance for your help.
Mark

Ancora
12-24-2006, 11:41 AM
Hi Mark:

Use onblur with the end date:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<title>Days Apart</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">

var today = new Date();
today.setHours(0,0,0,0);

function validate(isField){

var splitDate = isField.value.split("/");
var refDate = new Date(isField.value);
if (splitDate[0] < 1 || splitDate[0] > 12 || refDate.getDate() != splitDate[1] || splitDate[2].length != 4 || (!/^20/.test(splitDate[2])))
{
isField.value = "";
isField.focus();
return false;
}
isField.value = isField.value.replace(/^(\d{1}\/)/,"0\$1").replace(/(\d{2}\/)(\d{1}\/)/,"\$10\$2")
return refDate;
}

function verify(nForm){

var startDate = validate(nForm['fromDate']);
var endDate = validate(nForm['toDate']);
if (startDate && endDate)
{
if (startDate > endDate){alert('Invalid From Date'); return false}
if (startDate < today){alert('Invalid From Date'); return false}
var daysApart = Math.round((endDate-startDate)/86400000);
nForm['daysApart'].value = daysApart;
return true;
}
return false;
}

</script>
<style type="text/css">

body {background-color:#eae3c6;margin-top:60px}
form {width:250px;margin:auto}
fieldset {width:250px;background-color:#f0fff0;border:1px solid #87ceeb}
.submitBtn {font-family:tahoma;font-size:10pt;display:block;margin-left:auto;margin-right:auto;margin-top:5px;margin-bottom:5px}

</style>
<body>
<form action="">
<fieldset>
<legend>Days Apart</legend>
<label>From: (mm/dd/yyyy): <input type='text' size='9' name='fromDate'></label>
<label>To: (mm/dd/yyyy):&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='text' size='9' name='toDate' onblur="verify(this.form)"></label>
<label>Days Apart:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type='text' size='3' name='daysApart' readonly></label>
<input type='submit' name='submit' value="Submit" class='submitBtn'>
</fieldset>
</form>
</body>
</html>

sunkiss1
12-24-2006, 02:59 PM
Hi Mike,
Thanks for that. The funny thing is I tried "onfocus" with the field itself as well as a few other things but always came back with an error. I think the only thing I didn't try was the "onblur" with the end date field. Now I feel pretty stupid. I guess sometimes you can't see the forest for the trees.
Thanks,
Mark:)