[removed] calculate number of days in month for a

2019-01-14 04:16发布

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 year.

I haven't done this before on the client side, but it looks like a lot of controls like the jQuery DatePicker are doing that behind the scenes.

8条回答
甜甜的少女心
2楼-- · 2019-01-14 04:27

You can actually use this:

var curdate = new Date(); DaysMonth = 32 - new Date(curdate.getYear(), curdate.getMonth(), 32).getDate();

;)

查看更多
▲ chillily
3楼-- · 2019-01-14 04:28

As far as I know, there's no (neat) built-in function for that. I wrote this once:

// note that month is 0-based, like in the Date object. Adjust if necessary.
function getNumberOfDays(year, month) {
    var isLeap = ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0));
    return [31, (isLeap ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
}
查看更多
beautiful°
4楼-- · 2019-01-14 04:38
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);
             }
         }
     }

 }
查看更多
来,给爷笑一个
5楼-- · 2019-01-14 04:47

I am using this approach in my current project and found that I needed correct for round off errors. So instead of using monthLength in my code, I had to use this instead:

monthLength.toFixed(0)

For example if I have an object in which I am storing a text date field, it may look like this:

obj.month = theMonths[mm - 1] + " " + monthLength.toFixed(0) + ", " + obj.year;
查看更多
Evening l夕情丶
6楼-- · 2019-01-14 04:51
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();
}

alert(daysinMonthfromInput(2,2011));
查看更多
我想做一个坏孩纸
7楼-- · 2019-01-14 04:51

Here's the one liner. Assuming you are saying January=1, February=2 etc..(being normal) Here's the leap year example:

var y = 2012;
var m = 2;
var daysInMonth = new Date(y,m,1,-1).getDate();
查看更多
登录 后发表回答