...

View Full Version : add days to date entered in text box



david_stev
01-24-2006, 11:42 AM
I want to create a function that will take a date that has been entered in to text box 1 and a number of days.

The function should take the date value passed in from text box 1 convert it from a string to a date format and add the number of days passed in to the function to the date then output that value in the format dd/mm/yyyy to a second text box.

So far I have:

Text box that fires the function onBlur
<input type="text" name="txt_ad_date" value ="" onBlur="addDays(26/01/2006, 1)">


The function
function addDays(myDate, days) {
adDate = new Date(myDate);
adDate.setDate(adDate.getDate() + days);
document.frmPermVacReq.txt_closing_date.value = adDate;
}

This should result in
07/02/2006 in text box 2 (txt_closing_date)

what I actually get is Fri Jan 2 00:00:00 UTC 1970

Any Ideas???

vwphillips
01-24-2006, 02:22 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<title></title>
</head>
<script>



function addDays(myDate, days) {
adDate = new Date(myDate[0],myDate[1],(myDate[2]+days));
document.frmPermVacReq.txt_closing_date.value = Format(adDate.getDate())+'/'+Format(adDate.getMonth()+1)+'/'+adDate.getFullYear();
}

function Format(nu){
if (nu<10){ nu='0'+nu; }
return nu;
}
</script>
<body onload="addDays([2005,2,1], 4);">
<form name="frmPermVacReq" >
<input name="txt_closing_date" >
</form>
</body>

</html>

david_stev
01-24-2006, 02:32 PM
Thank you.

ammended the code slightly

<input type="text" name="txt_ad_date" value ="" onBlur="document.frmPermVacReq.txt_closing_date.value = addDays(document.frmPermVacReq.txt_ad_date.value, 14)">

function addDays(s, days) {
days=(document.frmPermVacReq.txt_ad_date.value*1)//-1
var dateArray = s.split('/');
sdate = new Date(dateArray[2],dateArray[1]-1,dateArray[0]);

if(dateArray[2].length<4){
alert("Please Enter The Year As A Four Digit Number\n\nExample:- 2002\n\nThank You")
return document.frmPermVacReq.txt_closing_date.value=""

}else{

var odate = new Date(sdate.getTime() + (days * 86400000));

alert(sdate.getDate());
alert(sdate.getMonth() + 1);
alert(sdate.getYear());

return odate.getDate() + '/' + (odate.getMonth()+1) + '/' + odate.getYear();
}
}

Could not have done this with out your help spent ages on google. Thank you.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum