johkar said:
I need to document.write out a select list populated with the dates for the
first and third Wednesday of each month. How do I get to the actual days?
<select name="mySelect">
<option value="Oct 1, 2003">Oct 1, 2003</option>
<option value="Oct 15, 2003">Oct 15, 2003</option>
....and so on for a continual 12 month period
</select>
In Javascript, the days of the week are represented by
the numbers 0-6, where 6="Sunday".
Given "firstDay" as the numerical day of the week of the
first day of the month, you can calculate the date of the
first Wednesday (3="Wednesday") as:
1+(3-firstDay+7)%7;
and the third Wednesday is 14 days later.
<html>
<body>
<form>
<script type="text/javascript">
var mName=["January","February","March","April","May","June", "July",
"August","September","October","November","Decembe r"];
var now=new Date();
now.setDate(1);
var month=now.getMonth();
var year=now.getFullYear();
var html="<select name=\"mySelect\">\n";
for(var i=0;i<12;i++){
// now is a Date object representing the first day
// of month "month" of year "year".
var firstDay=now.getDay();
var firstWed=1+(3-firstDay+7)%7;
var thirdWed=firstWed+14;
var monthName=mName[now.getMonth()];
html+="<option>"+monthName+" "+firstWed+", "+year+"</option>\n";
html+="<option>"+monthName+" "+thirdWed+", "+year+"</option>\n";
month++;
if(month==12){
month=0;
year++;
}
now=new Date(year,month,1);
}
html+="</select>\n";
document.write(html);
</script>
</form>
</body>
</html>