SoFunction
Updated on 2025-04-06

JavaScript uses select to implement date control

The code is very simple, so I won't talk much nonsense here. Just give it to everyone's source code

<!doctype html>
<html>
<head>
<title>Year, month, day</title>
</head>
<body onLoad="init()">
<select  onChange="swap_day()"></select>Year
<select  onChange="swap_day()"></select>moon
<select ></select>day
</body>
<script>
var month_big = new Array("1","3","5","7","8","10","12"); //Array containing all the big monthsvar month_small = new Array("4","6","9","11"); //Array containing all small months
//The function that initializes the option of the select control called when the page is loadedfunction init()
{
  var select_year = ("year"); //Get the drop-down list box with id "year"  var select_month = ("month"); //Get the drop-down list box with id "month"  var select_day = ("day"); //Get the drop-down list box with id "day"  
  //Initialize the year option from 1980 to 2000  for(var i = 1980; i <= 2000; i++)
  {
    select_year_option = new Option(i, i);
    select_year.(select_year_option);
  }
  
  //Initialize the month option from 1 to 12  for(var i = 1; i <= 12; i++)
  {
    select_month_option = new Option(i, i);
    select_month.(select_month_option);
  }
  
  //Calling swap_day function initialization date  swap_day();
}
//A function that determines whether the element obj is included in the array array. If it is included, it will return true. If it is not included, it will return false.function array_contain(array, obj)
{
  for (var i = 0; i < ; i++)
  {
    if (array[i] === obj)
    {
      return true;
    }
  }
  return false;
}

// Function to adjust date according to year and monthfunction swap_day()
{
  var select_year = ("year"); //Get the drop-down list box with id "year"  var select_month = ("month"); //Get the drop-down list box with id "month"  var select_day = ("day"); //Get the drop-down list box with id "day"  
  select_day. = 0; //Clear the original options in the date option before adjusting  var month = select_month.options[select_month.selectedIndex].value; //Get the selected month month  
  //If month is included in the month_big array, that is, the selected month is a large month, the date option is initialized to 31 days  if(array_contain(month_big, month))
  {
    for(var i = 1; i <= 31; i++)
    {
      select_day_option = new Option(i, i);
      select_day.(select_day_option);
    }
  }
  
  //If month is included in the month_small array, that is, the selected month is small, the date option is initialized to 30 days  else if(array_contain(month_small, month))
  {
    for(var i = 1; i <= 30; i++)
    {
      select_day_option = new Option(i, i);
      select_day.(select_day_option);
    }
  }
  
  //If month is 2, that is, the selected month is February, call the initFeb() function to initialize the date option  else
  {
    initFeb();   
  }
}
//Judge whether the year is a leap year. If it is a leap year, it will return true, otherwise it will return falsefunction isLeapYear(year)
{
  var a = year % 4;
  var b = year % 100;
  var c = year % 400;
  if( ( (a == 0) && (b != 0) ) || (c == 0) )
  {
    return true;
  }
  return false;
}

//Initialize the date option of February based on whether the year is leap year or not.function initFeb()
{
  var select_year = ("year"); //Get the drop-down list box with id "year"  var select_day = ("day"); //Get the drop-down list box with id "day"  var year = parseInt(select_year.options[select_year.selectedIndex].value); //Get the selected year and convert it to Int  
  // If it is a leap year, initialize the date option to 29 days  if(isLeapYear(year))
  {
    for(var i = 1; i <= 29; i++)
    {
      select_day_option = new Option(i, i);
      select_day.(select_day_option);
    }
  }
  
  // If it is not a leap year, initialize the date option to 28 days  else
  {
    for(var i = 1; i <= 28; i++)
    {
      select_day_option = new Option(i, i);
      select_day.(select_day_option);
    }
  }
}
</script>
</html>

The above is the entire content of this article, I hope you like it.