PDA

View Full Version : How do I automatically increment the date to 2009 if they enter a date earlier?



kuau
10-29-2008, 01:47 AM
How do I get the year to increment automatically if the user enters date earlier than today?

They would be entering dates for renting a car, so if they enter Feb 22, 2008 I would like it to change to 2009 automatically for both the pickup and drop-off dates.

I have this code:


function Pi_Month() {
Pickup_Month = document.rental.Pick_Month.value;
document.rental.Drop_Month.value = Pickup_Month;
}
function Pi_Day() {
Pickup_Day = document.rental.Pick_Day.value;
document.rental.Drop_Day.value = Pickup_Day;
}
function Pi_Year() {
Pickup_Year = document.rental.Pick_Year.value;
document.rental.Drop_Year.value = Pickup_Year;
}

And tried adding this but it did nothing:


if (document.rental.Pick_Month.value) < (now.getMonth()) {
Pickup_Year = document.rental.Pick_Year.value+1;
document.rental.Drop_Year.value = Pickup_Year;
}

I don't speak JS very well (at all). Thanks for any help! :)

jscheuer1
10-29-2008, 02:21 AM
if (document.rental.Pick_Month.value) < (now.getMonth()) {
Pickup_Year = document.rental.Pick_Year.value+1;
document.rental.Drop_Year.value = Pickup_Year;
}

I thought the year, not the month was at issue. However, a closer reading of your post seems to indicate that no dates prior to the present are acceptable.

Assuming the rest of the code is OK and produces a Date Object for the pickup date, you should be able to check the value of both now and the picked date, bringing it to the present year if it is less than now, regardless of what year it is. It may still be a prior month though, or even a date in the current month prior to now. That can be checked and dealt with too. But I'm not really clear on your other code, and though I will look further at it, I think you haven't shown enough for me to be clear on how you arrived at the pickup date or what type of object it is. Anyways, let's say we have a Date Object, someDate. It doesn't matter how you arrive at someDate, as long as it is a Date Object. Then we can do:


var now = new Date();
if (valueOf(someDate) < valueOf(now))
someDate.setFullYear(now.getFullYear());
while (valueOf(someDate) < valueOf(now))
someDate.setMonth(someDate.getMonth() + 1);

That way, if it is prior to now, it will be set to the current year. If it is still prior to now, it's month value will be incremented until it is no longer prior to now.

kuau
10-29-2008, 02:39 AM
Dear John:

I was hoping you were around somewhere. Most people reserve cars well in advance so anyone entering February at this point will mean 2009 even if they forget to change the year. So I'm just trying to avoid a confusing error message for the user. Here is the entire js file in case that helps: (I inherited it)


<!--

function goto_URL(object) {
window.location.href = object.options[object.selectedIndex].value;
}

function Pick_Loc() {
Pickup_Loc = document.rental.Pick_Location.value;
document.rental.Drop_Location.value = Pickup_Loc;
}

function Drop_Loc() {
Dropoff_Loc = document.rental.Drop_Location.value;
/* if(Dropoff_Loc == "Maui - Kahului Airport") {
document.rental.Pick_Location.value = Dropoff_Loc;
}
if(Dropoff_Loc == "Maui - Kaanapali") {
document.rental.Pick_Location.value = Dropoff_Loc;
}*/
if(Dropoff_Loc == "Kauai - Lihue Airport") {
document.rental.Pick_Location.value = Dropoff_Loc;
}
}

function Pi_Month() {
Pickup_Month = document.rental.Pick_Month.value;
document.rental.Drop_Month.value = Pickup_Month;
}
function Pi_Day() {
Pickup_Day = document.rental.Pick_Day.value;
document.rental.Drop_Day.value = Pickup_Day;
}
function Pi_Year() {
Pickup_Year = document.rental.Pick_Year.value;
document.rental.Drop_Year.value = Pickup_Year;
}


function validate() {

now = new Date;
day2 = addDays(new Date(),2);


date0 = "12/15/2009";
date1 = document.rental.Pick_Month.value+"/"+document.rental.Pick_Day.value+"/"+document.rental.Pick_Year.value;
date2 = document.rental.Drop_Month.value+"/"+document.rental.Drop_Day.value+"/"+document.rental.Drop_Year.value;
date3 = (now.getMonth()+1)+"/"+now.getDate()+"/"+now.getFullYear();
date4 = (day2.getMonth()+1)+"/"+day2.getDate()+"/"+day2.getFullYear();

date_lim = Date.parse(date0);
date_num1 = Date.parse(date1);
date_num2 = Date.parse(date2);
date_num3 = Date.parse(date3);
date_num4 = Date.parse(date4);

date_diff = date_num2 - date_num1;
date_diff2 = date_num1 - date_num4;

rtime1 = document.rental.Pick_Time.value;
rtime2 = document.rental.Drop_Time.value;

rtime_diff = rtime2 - rtime1;

/* if (document.rental.Pick_Month.value) < (now.getMonth()) {
Pickup_Year = document.rental.Pick_Year.value+1;
document.rental.Drop_Year.value = Pickup_Year;
} */
if (document.rental.Pick_AP[0].checked) {
ptime1 = document.rental.Pick_AP[0].value;
}
if (document.rental.Pick_AP[1].checked) {
ptime1 = document.rental.Pick_AP[1].value;
}
if (document.rental.Drop_AP[0].checked) {
ptime2 = document.rental.Drop_AP[0].value;
}
if (document.rental.Drop_AP[1].checked) {
ptime2 = document.rental.Drop_AP[1].value;
}

if (date_num1 >= date_lim) {
alert("We are sorry, but at this point we are not able to process car pick-up dates beyond December 15, 2009.");
return false;
}


if (date_diff < 0) {
alert("Your rental period needs to be at least 1 day. Please check you pick-up and drop-off date.");
return false;
}

if (date_diff2 < 0) {
alert("Your pick-up date needs to be at least 2 days later than today in order to book thru our system.\nYou can try to call us and we may be able to help you right away 1-866-300-7116. But we won't guarantee it!");
return false;
}

if (date_diff == 0 && rtime_diff < 0) {
if ((ptime1 == 'PM' && ptime2 == 'AM') || (ptime1 == 'AM' && ptime2 == 'AM') || (ptime1 == 'PM' && ptime2 == 'PM')) {
alert("Your drop-off time canīt be earlier than your pick-up time on the same day!\n Please change your pick-up or drop-off date.");
return false;
}
}

Pickup_Loc = document.rental.Pick_Location.value;
Dropoff_Loc = document.rental.Drop_Location.value;

if(Pickup_Loc == "Big Island - Kona Airport" || Pickup_Loc == "Big Island - Hilo Airport") {
if(Dropoff_Loc != "Big Island - Kona Airport" && Dropoff_Loc != "Big Island - Hilo Airport") {
alert("Only Big Island - Kona Airport and Hilo Airport are possible for drop off location.");
return false;
}
}

if(Dropoff_Loc == "Big Island - Kona Airport" || Dropoff_Loc == "Big Island - Hilo Airport") {
if(Pickup_Loc != "Big Island - Kona Airport" && Pickup_Loc != "Big Island - Hilo Airport") {
/* alert("Only Big Island - Kona Airport and Hilo Airport are possible for pick-up location.");*/
alert("You must pick-up and drop-off the car on the same island.");
return false;
}
}

if(Pickup_Loc == "Oahu - Honolulu Airport" || Pickup_Loc == "Oahu - Honolulu City") {
if(Dropoff_Loc != "Oahu - Honolulu Airport" && Dropoff_Loc != "Oahu - Honolulu City") {
alert("Only Oahu - Honolulu Airport and Honolulu City are possible for drop off location.");
return false;
}
}

if(Dropoff_Loc == "Oahu - Honolulu Airport" || Dropoff_Loc == "Oahu - Honolulu City") {
if(Pickup_Loc != "Oahu - Honolulu Airport" && Pickup_Loc != "Oahu - Honolulu City") {
alert("You must pick-up and drop-off the car on the same island.");
return false;
}
}

if(Pickup_Loc == "Maui - Kaanapali" || Pickup_Loc == "Maui - Kahului Airport") {
if(Dropoff_Loc != "Maui - Kaanapali" && Dropoff_Loc != "Maui - Kahului Airport") {
alert("Only Maui - Kaanapali and Kahului Airport are possible for drop off location.");
return false;
}
}

if(Dropoff_Loc == "Maui - Kaanapali" || Dropoff_Loc == "Maui - Kahului Airport") {
if(Pickup_Loc != "Maui - Kaanapali" && Pickup_Loc != "Maui - Kahului Airport") {
alert("You must pick-up and drop-off the car on the same island.");
return false;
}
}


Car_Select = document.rental.Car_Class.value;
if(Car_Select == "PREF") {
alert("Please select the vehicle class before you do the price check.");
return false;
}
}

function addDays(myDate,days) {
return new Date(myDate.getTime() + days*24*60*60*1000);
}

//-->


You can see what happens when you enter (for example) Feb 2008 here:

www.carrentalhawaii.com

Nothing like the real thing to explain better than I can! Thanks for your help. :)

jscheuer1
10-29-2008, 04:55 AM
I would think this line would take care of that:


if (date_diff2 < 0) {
alert("Your pick-up date needs to be at least 2 days later than today in order to book thru our system.\nYou can try to call us and we may be able to help you right away 1-866-300-7116. But we won't guarantee it!");
return false;
}

You can't really change the year on someone without risking arriving at a date that they didn't intend. By just letting the existing validation take care of it, you ensure that the person will be consciously selecting an acceptable date, rather than having one foisted upon them without their direct participation.

Say they pick a month where you want to advance the year and you do, then they change the month to one that would have been acceptable in the current year, the year is already advanced, so they may actually wind up with a date they didn't intend.

kuau
10-29-2008, 05:40 AM
While it is true that the error message points out if they forget to change the year, the guy who owns the site wants it to advance the way I described. Because the date always defaults to 2 days after today's date, I can't imagine how a user could enter a legitimate month that precedes the current month that would be valid without incrementing the year. In any case, that is what the owner wants so it is not my place to talk him out of it, but to get it to work. It's his site. I'd really appreciate some help to that end. Thanks very much. :)