Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

add days to date

Status
Not open for further replies.

davidste

Programmer
Mar 7, 2004
15
GB
I have one text box I want the user to fill out with a date, I then have a second text box i want to be poulated with the date entered in the first text box plus 14 days.

So far I have the following

function addDays(myDate,days) {
var myDate = new date();

myDate.setDate(myDate.getDate() + days);

document.frmPermVacReq.txt_closing_date.value = myDate;
}


The first text box has an onBlur action as follows:
<input type="text" name="txt_ad_date" value ="" onBlur="addDays(txt_ad_date, 14)">

The second txt box is coded as follows:
<input type="text" name="txt_closing_date" value="" >

Every time I try this I get the following error:
'date is undefined'
at the following line of code
var adDate = new date();

any ideas???
 
Javascript is case sensitive, make the following change:
Code:
var myDate = new [!]D[/!]ate();

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
Thank you.

I have changed that but now I have another problem.

Have changed the function to reflect the changes you advised and added a few alert boxes to display values of vars.

function addDays(myDate,days) {
var closeDate = new Date();
var adDate = new Date();

adDate.setDate(myDate)

alert(adDate);
alert(days);

closeDate.setDate(adDate + days);

document.frmPermVacReq.txt_closing_date.value = closeDate;
}

The second text box now displays Nan and adDate comes up as '[object]' in the alert.

Any Ideas?
 
The second alert (the variable "days") should never give you NaN (not "Nan", incidentally) - assuming the code used to call the function is exactly as you showed us originally, as you are explicitly passing the number 14. Are you still doing this, or has the code changed since the code you showed us?

The alert for adDate could well appear as "[object]" as it is a Date object. What do you actually want to see? If you want to see a specific date format, then use whichever method of the Date object outputs the format that you want to see.

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top