The code

For a wide range of years, a given year is a leap year if it is divisible by 4 but not divisible by 100, unless it is
divisible by 400. It works for the recent past (back to the Gregorian calendar reformation of 1752, anyway) and by definition for all future years (though the definition may have to be amended towards the end of the next millenium) If you care about historical dates, or astronomical time-keeping, you probably know about all of the various complications anyway.
For details please refer to this FAQ
published by NIST.

<script>
var myyear  = '2004' 
var mymonth = 'January'
var mydate = '1' 

function makelist(num)
{
   document.forms["dmk_date_entry"].dmk_leapyear_date.options.length=0;
   for (i = 0; i < num; i++ ) {
      document.forms["dmk_date_entry"].dmk_leapyear_date.options[i] = new Option(i+1)
   }
}

function datelist()
{
  myyear=document.forms["dmk_date_entry"].dmk_leapyear_year.options[
     document.forms["dmk_date_entry"].dmk_leapyear_year.selectedIndex  
  ].text
  mymonth=document.forms["dmk_date_entry"].dmk_leapyear_month.selectedIndex  + 1
  switch (mymonth) {
     case 1: case 3: case 5: case 7: case 8: case 10: case 12:
        makelist(31) 
        break
     case 2:
        comment = "This works for a wide range of years, but not all"
        if (
             ((myyear % 400) == 0)||
             ( ((myyear % 4) == 0) && ((myyear % 100) != 0) )
           )
        { 
            makelist(29) 
        }
        else {
            makelist(28) 
        }
        break
     default:
        makelist(30) 
  }
  mydate=document.forms["dmk_date_entry"].dmk_leapyear_date.selectedIndex + 1;
  savedate()
}

function savedate() 
{
    mydate=document.forms["dmk_date_entry"].dmk_leapyear_date.selectedIndex + 1;
    datestr = myyear + '-' + mymonth + '-' + mydate
    document.forms["dmk_date_entry"].dmk_leapyear_savedate.value=datestr
}

this.onload = datelist();

</script>

This would be invoked in the HTML using something similar to the following example. The number of days
in the 'date' list is changed each time the year or month selection is changed; the value of the
form element is updated when a date is selected.

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Lines and paragraphs break automatically.

More information about formatting options