Javascript Check Leap Year Date

I tried to find a solution in existing stackoverflow answers, but I did not find a suitable answer ...

I have 3 fields.

  • Year select
  • Month select
  • the date select

If you select a leap year, the February date of the month changes by 29 days .. But it still shows 28 days. I don’t know what the problem is in my code.

var numDays = {
        '1': 31, '2': 28, '3': 31, '4': 30, '5': 31, '6': 30,
        '7': 31, '8': 31, '9': 30, '10': 31, '11': 30, '12': 31
    };

    function setDays(oMonthSel, oDaysSel, oYearSel)
    {
        var nDays, oDaysSelLgth, opt, i = 1;
        nDays = numDays[oMonthSel[oMonthSel.selectedIndex].value];
        if (nDays == 28 && oYearSel[oYearSel.selectedIndex].value % 4 == 0)
            ++nDays;
        oDaysSelLgth = oDaysSel.length;
        if (nDays != oDaysSelLgth)
        {
            if (nDays < oDaysSelLgth)
                oDaysSel.length = nDays;
            else
                for (i; i < nDays - oDaysSelLgth + 1; i++)
                {
                    opt = new Option(oDaysSelLgth + i, oDaysSelLgth + i);
                    oDaysSel.options[oDaysSel.length] = opt;
                }
        }
        var oForm = oMonthSel.form;
        var month = oMonthSel.options[oMonthSel.selectedIndex].value;
        var day = oDaysSel.options[oDaysSel.selectedIndex].value;
        var year = oYearSel.options[oYearSel.selectedIndex].value;
        oForm.dob.value = month + '/' + day + '/' + year;
    }
    var min = new Date().getFullYear(),
            max = min - 13,
            select = document.getElementById('year');
    for (var i = (min - 100); i <= max; i++) {
        var opt = document.createElement('option');
        opt.value = i;
        opt.innerHTML = i;
        select.appendChild(opt);
    }
    date = document.getElementById('day');
    for (var i = 1; i <= 31; i++) {
        var opt = document.createElement('option');
        opt.value = i;
        opt.innerHTML = i;
        date.appendChild(opt);
    }
    jQuery('.month_sel,.day_sel,.year_sel').selectpicker({
        size: 8
    });
Run codeHide result

<select class="year_sel common_sel" name="year" id="year" onchange="setDays(month, day, this)">
<option value="">Year</option>
</select>
<select name="month" id="month" onchange="setDays(this, day,year)" class="month_sel common_sel">
<option value="">Month</option>
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select class="day_sel common_sel" name="day" id="day" onchange="setDays(month, this, year)">
<option value="">Day</option>
</select>
<input type="hidden" name="dob" value="" />
Run codeHide result
+4
source share
4 answers

You can simply use the built-in Date object. Just set it to February 29th for a given year. Then check if it is actually set to February (leap year), or if it has switched to March (not a leap year).

var year = (new Date()).getFullYear();
new Date(year + "-02-29").getMonth() === 1;
+2
source

This bit is invalid:

if (nDays == 28 && oYearSel[oYearSel.selectedIndex].value % 4 == 0)
    ++nDays;

, . : 4, 100, 400, .

,

if (nDays == 28) {
    var year = oYearSel[oYearSel.selectedIndex].value;
    if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) {
        nDays = 29;
    }
}

, 2000 ; , 75% (1900 2100).

+1

Date:

    Date.prototype.isLeapYear = function() {
      if (0 == this.getFullYear() % 400) return true;
      if (0 == this.getFullYear() % 100) return false;
      return (0 == this.getFullYear() % 4) ? true : false;
    }

, !

script , , :

    var dtTest = new Date();
    alert(dtTest + " Is leapyear = " + dtTest.isLeapYear());

:

    var dtTest = new Date(2016,8,9);
    alert(dtTest + " Is leapyear = " + dtTest.isLeapYear());
0

:

function getDaysInMonth(year, month){
  return new Date(year, month+1, 0).getDate();
}

console.log(getDaysInMonth(2016,1));
console.log(getDaysInMonth(2010,1));
console.log(getDaysInMonth(2000,1));
console.log(getDaysInMonth(2100,1));
Hide result
0

Source: https://habr.com/ru/post/1650650/


All Articles