[removed] calculate number of days in month for a given year

前端 未结 8 1888
一个人的身影
一个人的身影 2020-12-05 12:12

I have a HTML page with 3 dropdowns for the month, day and year and I was wondering if there was a way to populate the month drop down properly depending on the month and ye

8条回答
  •  粉色の甜心
    2020-12-05 12:32

    Date.prototype.daysinMonth= function(){
    var d= new Date(this.getFullYear(), this.getMonth()+1, 0);
    return d.getDate();
    };
    
    function daysinMonthfromInput  (month, year) {
         return (new Date(year, month - 1, 1)).daysinMonth();
    };
    function fillallday (elem, month, year) {
         var options = null;
         var elementExists = document.getElementById(elem);
    
         if (elementExists != null) {
    
             this.removeOptions(elementExists);
             var opt = document.createElement('option');
             opt.value = "";
             opt.innerHTML = "---Day---";
             elementExists.appendChild(opt);
             if (month != "") {
                 if (typeof (year) === "undefined") {
                     year = new Date().getFullYear();
                 }
                 if (year == "") {
                     year = new Date().getFullYear();
                 }
                 var days = daysinMonthfromInput(month, year);
                 for (var i = 1; i <= days; i++) {
                     var opt = document.createElement('option');
                     opt.value = i;
                     opt.innerHTML = i;
                     elementExists.appendChild(opt);
                 }
             }
         }
    
     }
    

提交回复
热议问题