Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q1520 How do I display the current date as selected options in year, month and day select options?

You are here: irt.org | FAQ | JavaScript | Date | Q1520 [ previous next ]

Ty:

<html>
<head>
<script language="JavaScript"><!--
var now = new Date();
var day = now.getDate();
var month = now.getMonth()+1; // numbered from 0
var year = now.getYear();
if (year < 1900) year += 1900; // or use getFullYear() in v4 browsers
/* comment the following line out to remove the display of date */
document.write(now.toString()+'<br>'+year+'/'+month+'/'+day);

function setDrops(theForm) {
   // I assume Day and months are all there and start with "Select a Day and Select a month
   theForm.daySelect.selectedIndex = day;
   theForm.monthSelect.selectedIndex = month;
  // now the year will be slightly different
   var found = 0;
   for(i=1;i<theForm.yearSelect.options.length;i++) {
      if (theForm.yearSelect.options[i].value == year) {
         found =i;
         break;
      }
   }
   theForm.yearSelect.selectedIndex =  found;
}
//--></script>
</head>
<body onLoad="setDrops(document.myForm)">
<form name="myForm">
<select name="yearSelect">
<option value="">Year
<option value="1999">1999
<option value="2000">2000
<option value="2001">2001
</select>

<select name="monthSelect">
<option value="">Month
<option value="1">1
<option value="2">2
<option value="3">3
<option value="4">4
<option value="5">5
<option value="6">6
<option value="7">7
<option value="8">8
<option value="9">9
<option value="10">10
<option value="11">11
<option value="12">12
</select>

<select name="daySelect">
<option value="">Day
<option value="1">1
<option value="2">2
<option value="3">3
<option value="4">4
<option value="5">5
<option value="6">6
<option value="7">7
<option value="8">8
<option value="9">9
<option value="10">10
<option value="11">11
<option value="12">12
<option value="13">13
<option value="14">14
<option value="15">15
<option value="16">16
<option value="17">17
<option value="18">18
<option value="19">19
<option value="20">20
<option value="21">21
<option value="22">22
<option value="23">23
<option value="24">24
<option value="25">25
<option value="26">26
<option value="27">27
<option value="28">28
<option value="29">29
<option value="30">30
<option value="31">31
</select>
</form>
</body>

©2018 Martin Webb