SoFunction
Updated on 2025-02-28

JS realizes date cascade effect


    <div>
        <select name="YYYY" onchange="YYYYMM()">
<option value="">Please select Year</option>
        </select>
        <select name="MM" onchange="MMDD()">
<option value="">Select month</option>
        </select>
        <select name="DD">
<option value="">Select day</option>
        </select>
    </div>

    <script language="JavaScript">
        function () {
            strYYYY = document.;
            strMM = document.;
            strDD = document.;
            MonHead = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

//First assign content to the annual drop-down box
            var y = new Date().getFullYear();
            var str = (0, - 9);
for (var i = (y - 30); i < (y + 30); i++) //This year shall prevail, first 30 years and next 30 years
            {
str += "<option value='" + i + "'> " + i + "year" + "</option>\r\n";
            }
            document. = str + "</select>";

//The drop-down box for the month of the fu
            var str = (0, - 9);
            for (var i = 1; i < 13; i++) {
str += "<option value='" + i + "'> " + i + "month" + "</option>\r\n";
            }
            document. = str + "</select>";

            document. = y;
            document. = new Date().getMonth() + 1;
            var n = MonHead[new Date().getMonth()];
            if (new Date().getMonth() == 1 && IsPinYear(YYYYvalue)) n++;
writeDay(n); //Fun Date drop-down box
            document. = new Date().getDate();
        }

function YYYYMM(str) //The date changes when the year changes (mainly to judge the leap year)
        {
            var MMvalue = document.[document.].value;
            if (MMvalue == "") { = strDD; return; }
            var n = MonHead[MMvalue - 1];
            if (MMvalue == 2 && IsPinYear(str)) n++;
            writeDay(n)
        }

function MMDD(str) //Date linkage when the month changes
        {
            var YYYYvalue = document.[document.].value;
            if (str == "") { = strDD; return; }
            var n = MonHead[str - 1];
            if (str == 2 && IsPinYear(YYYYvalue)) n++;
            writeDay(n)
        }

function writeDay(n) //Write date according to conditions drop-down box
        {
            var s = (0, - 9);
            for (var i = 1; i < (n + 1); i++)
s += "<option value='" + i + "'> " + i + "day" + "</option>\r\n";
            document. = s + "</select>";
        }

function IsPinYear(year)//Judge whether it is a leap year
        {
            return (0 == year % 4 && (year % 100 != 0 || year % 400 == 0))
        }