View Full Version : Date selection field population problems

Gr3y Drag0n
01-28-2005, 06:46 PM
Hey all, I am having a bit of difficulty sorting out where the problem may be in this script. Here is what is SUPPOSE to happen. When the page loads the drop downs should populate the year and month 6 months into the future. The form will be used as a delivery form. The days of the month should populate onChange. What happens is that the months and year do infact populate correctly but the days do not. It seems to be giving me the days from either the previous or following month rather than the selected month. This is not my code, it was just handed to me so deconstructing it and understanding it are giving me a bit of trouble. I hope you all can help.


<script language="JavaScript">
<!-- Original: Ben McFarlin (mcfarlin@netscape.net) -->
<!-- Web Site: http://sites.netscape.net/mcfarlin -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
var currDate = new Date();
var currYear = parseInt(currDate.getFullYear());
var nextYear = currYear + 1;
var currMonth = currDate.getMonth() + 1;
var currDay = currDate.getDate();

function populate(objForm,selectIndex) {
var m = parseInt(objForm.month.options[objForm.month.selectedIndex].value);
timeA = new Date (objForm.year.options[objForm.year.selectedIndex].text, // Chosen year
m + 1 , // Next Month
1); // [Chosen Year] 1, [Chosen Year]
timeB = new Date(timeA - 86400000); // Back one day
var daysInMonth = timeB.getDate(); // Number of days in the chosen month

for(var i = 0; i < 31; i++) {
objForm.day.options[i] = null;

if(m == currMonth) {
for(var i = (currDay + 1), j = 0; i <= (daysInMonth); i++, j++) {
document.form1.day.options[j] = new Option(i);
} else {
for (var i = 0; i < daysInMonth; i++) {
objForm.day.options[i] = new Option(i+1);

document.form1.day.options[0].selected = true;

function getYears() {
var years;
if(currMonth > 5) {
document.form1.year.options[0] = new Option(currYear);
document.form1.year.options[1] = new Option(nextYear);
} else if(currMonth == 12) {
document.form1.year.options[0] = new Option(nextYear);
} else {
document.form1.year.options[0] = new Option(currYear);

var months = new Array("", "January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December");
var blah;
if(currMonth == 12 && currDay == 31) {
blah = new Date(nextYear,
1); // January, 1 [Next Year]
} else {
blah = new Date(currYear,
currMonth + 1,
1); // [Next Month], 1, [This Year]

var blahblah = new Date(blah - 86400000); // Go back one day

// Fill in the months
for(var i = currMonth, j = 0; j < 6; i++, j++) {
if(i > 12) { i = 1; }
document.form1.month.options[j] = new Option(months[i], i);

// Fill in the days
for(var i = (currDay + 1), j = 0; i <= (blahblah.getDate()); i++, j++) {
document.form1.day.options[j] = new Option(i);
// End -->


<table border=0 width="100%">
<td colspan="2"><b>Date and Time:<font color="#FF0000">*</font></b></td>
<td>Date: </td>
<select name=year onChange="populate(this.form,this.form.month.selectedIndex);">
<select name=month onChange="populate(this.form,this.selectedIndex);">
<select name=day>
<td>Time :</td>
<select name="time">
<option value="" selected>Time</option>
<option value="8:00 am">8:00 am</option>
<option value="8:30 am">8:30 am</option>
<option value="9:00 am">9:00 am</option>
<option value="9:30 am">9:30 am</option>
<option value="10:00 am">10:00 am</option>
<option value="10:30 am">10:30 am</option>
<option value="11:00 am">11:00 am</option>
<option value="11:30 am">11:30 am</option>
<option value="12:00 pm">12:00 pm</option>
<option value="12:30 pm">12:30 pm</option>
<option value="1:00 pm">1:00 pm</option>
<option value="1:30 pm">1:30 pm</option>
<option value="2:00 pm">2:00 pm</option>
<option value="2:30 pm">2:30 pm</option>
<option value="3:00 pm">3:00 pm</option>
<option value="3:30 pm">3:30 pm</option>
<option value="4:00 pm">4:00 pm</option>
<option value="4:30 pm">4:30 pm</option>
<option value="5:00 pm">5:00 pm</option>
<option value="5:30 pm">5:30 pm</option>
<option value="6:00 pm">6:00 pm</option>
<option value="6:30 pm">6:30 pm</option>
<option value="7:00 pm">7:00 pm</option>
<option value="7:30 pm">7:30 pm</option>
<option value="8:00 pm">8:00 pm</option>
<option value="8:30 pm">8:30 pm</option>
<option value="9:00 pm">9:00 pm</option>
<option value="9:30 pm">9:30 pm</option>

here is the original script that was previously modified. (http://javascript.internet.com/calendars/date-menu.html)

Willy Duitt
01-28-2005, 07:44 PM
No offense to Javascript Kit but there have been better scripts which do what you want previously posted on the forum... Besides, tryng to fix unknown errors in an inferior, yet modified script, would be redundant when better ones are available...

Try looking here: