I have the following need. I have users entering an action that has been performed, so the date is always current or past, never future. I have a drop-down selection for day of the week, and text entry for date in the form “mm/dd/yyyy” in a text input. Both default to the current, gotten from the computer (server) at time of entry.
I would like that when the user selects a day of the week the date is changed to suit that selection, and always backward. Today is Saturday: if the user selects Thursday subtract 2 days, if he selects Sunday subtract 6 days, etc.
I am not even up to novice in javascript, so the following is probably pretty pathetic, and of course it doesn’t work. If somebody would be so kind as to sho me where I went wrong I would be delighted.
[CODE]
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<html>
<head>
<title>Testing</title>
<Script Language=JavaScript>
var theForm = “”;
function DaysInFeb(year)
{
if (year < 0) year++;
year = year + 4800;
if ((year % 4) == 0)
{ if ((year % 100) == 0)
{ if ((year % 400) == 0)
return(29);
else return(28);
}
else return(29);
}
else return(28);
}
function DaysInMonth(month, year)
{
if(month == 2)
{ return DaysInFeb(year);
}
elseif(month == 4 || month == 6 || month == 9 || month == 11)
{ return(30);
}
else return(31);
}
function showNewDate(prvDay)
{
theForm = document.forms.FormOne;
dtArr = theForm.Date.value.split(“/”);
Cmth = dtArr[0];
Cday = dtArr[1];
Cyr = dtArr[2];
newDay = theform.Wkday.value;
chg = newDay – prvDay;
if (chg > 0)
{ chg = chg – 7;
}
Cday = Cday – chg;
if (Cday < 1)
{ if (Cmth == 1)
{ Cmth = 12;
Cyr–;
}
else Cmth–;
Cday = Cday + DaysInMonth(Cmth,Cyr);
}
if (Cmth < 10)
DateStr = “0”+Cmth+”/”;
else DateStr = Cmth+”/”
if (Cday < 10)
DateStr = DateStr+”/0″+Cday+”/”+Cyr;
else DateStr = DateStr+”/”+Cday+”/”+Cyr;
theForm.Date.value = DateStr;
}
</Script>
</head>
<body>
<center>
<form method=’post’ name=’FormOne’ action=’sx_inpassn.php’>
<table border=0 cellpadding=0 cellspacing=0>
<td>Day:</td><td>
<select name=’Wkday’style=’width:100px;’ onchange=’showNewDate(7);’>
<option value=1>Sunday</option>
<option value=2>Monday</option>
<option value=3>Tuesday</option>
<option value=4>Wednesday</option>
<option value=5>Thursday</option>
<option value=6>Friday</option>
<option value=7 selected>Saturday</option>
</select></td></tr>
<td>Date:</td><td>
<input type=text name=’Date’ style=’width:100px’ maxlength=10 value=’07/16/2005′>
</td></tr></table>
</form>
</center>
</body>
</html>