Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populate combo boxes with today date

Status
Not open for further replies.

sabavno

Programmer
Jul 25, 2002
381
CA
Hi,

I have three combo boxes on my form. First contains the month selection, second - day's, third - year.

(E.g. <SELECT NAME=RelMonth>
<OPTION VALUE=1>Jan
<OPTION VALUE=2>Feb
<OPTION VALUE=3>Mar
<OPTION VALUE=4>Apr
<OPTION VALUE=5>May
<OPTION VALUE=6>Jun
<OPTION VALUE=7>Jul
<OPTION VALUE=8>Aug
<OPTION VALUE=9>Sep
<OPTION VALUE=10>Oct
<OPTION VALUE=11>Nov
<OPTION VALUE=12>Dec

</SELECT>)


On the loadForm, I want these boxes have a selection corresponding to the today's date.

Please advise if it is dueable.

Thanks.
 
It is, after you've built the SELECTS try something like :

<html>
<body>
<form name="myForm">
<select name="days">
<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>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">10</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select name="months">
<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>
<select name="years">
<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>
</select>
</form>
<script language="javascript">
var myDate=new Date();
var day = myDate.getDate();
var month = myDate.getMonth();
var yy = myDate.getYear();
var year = (yy < 1000) ? yy + 1900 : yy;
document.myForm.days.options[day -1].selected=true;
document.myForm.months.options[month].selected=true;
for (count=0;count<document.myForm.years.length;count++)
{
if (document.myForm.years.options[count].value==year)
{
document.myForm.years.options[count].selected=true;
}
}
</script>
</body>
</html>
 
you could also shorten the script code like so:
Code:
<script language="javascript">
var myDate=new Date();
var day = myDate.getDate();
var month = myDate.getMonth();
var yy = myDate.getFullYear();
myForm.days.value = day;
myForm.months.value = month;
myForm.years.value = yy;
</script>

-kaht

banghead.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top