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

01-24-2006, 12:42 PM
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???

Mr J
01-24-2006, 03:04 PM
Please try the following

<script type="text/javascript">
function calc_date() {

dateArray = s.split('/')
sdate = new Date(dateArray[2],dateArray[1]-1,dateArray[0])

alert("Please Enter The Year As A Four Digit Number\n\nExample:- 2002\n\nThank You")
return document.f2.date_to.value=""
odate = new Date(sdate.getTime() + (days * 86400000));
document.f2.date_to.value=odate.getDate() + '/' + (odate.getMonth()+1) + '/' + odate.getYear()

<form name="f2">
<P><table border=1>
<tr><td>Start Date</td><td>Number of days</td><td>End Date</td></tr>
<tr><td><input type="text" name="date_from" size="10" value="24/01/2006"></td>
<td align="center"><input type="text" name="daynums" size="4" maxlength="4" value="5"></td>
<td><input type="text" name="date_to" size="10"></td></tr>
<tr><td colspan=3 align=center>
<input type="button" value="calculate" onclick="calc_date()">