ChompGator Posted April 20, 2008 Share Posted April 20, 2008 Would anyone know a script for having three drop down boxes One for: Year One for: Month One for: Day And the data populates based on the month selected. Ie: If I select 2008 > Then February > It pre-populates 29 days because this year February only had 29 days. If I selected 2008 > Then March > Then 31 days are pre-populated into the drop down because March had 31 days. Anyone have a simple script to do that? Quote Link to comment Share on other sites More sharing options...
optikalefx Posted April 21, 2008 Share Posted April 21, 2008 no one is gonna write it for you but we can give insight on how to do it you can make use of the javascript built in date functions. Then based on what was clicked, use the functions to find out the year, and using math and the fact that each month as the same number of days every 4 years, you can calculate what this year will have. have fun making it Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 21, 2008 Share Posted April 21, 2008 ...and using math and the fact that each month as the same number of days every 4 years, you can calculate what this year will have. Ah, but leap year doesn't occur every four years. It occurs every four years EXCEPT on any year that is divisible by 100 (unless it is also divisible by 400). 2000 = Leap year 2100 = Not a leap year 2200 = Not a leap year 2300 = Not a leap year 2400 = Leap year Anyway, here is some code for ya <html> <head> <script type="text/javascript"> var monthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; function changeDays() { var year = parseInt(document.getElementById('year').value); var month = parseInt(document.getElementById('month').value); var day = document.getElementById('day'); //Determine number of days in the month if (month==2 && (year%4==0 && (year%100!=0 || year%400==0))) { //Feb in a leap year var daysInMonth = 29; } else { var daysInMonth = monthDays[month-1]; } //Add days (if needed) while (daysInMonth>day.length) { day.options[day.length] = new Option((day.length+1), (day.length+1)); } //remove days (if needed) day.length = daysInMonth; return; } </script> </head> <body onload="changeDays();"> Month: <select id="month" name="month" onchange="changeDays()"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> <option value="11">11</option> <option value="12">12</option> </select> Day: <select id="day" name="day"></select> Year: <select id="year" name="year" onchange="changeDays()"> <option value="2000">2000</option> <option value="2001">2001</option> <option value="2002">2002</option> <option value="2003">2003</option> <option value="2004">2004</option> <option value="2005">2005</option> <option value="2006">2006</option> <option value="2007">2007</option> <option value="2008">2008</option> <option value="2009">2009</option> <option value="2010">2010</option> </select><br> </body> </html> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.