11

我有一个 HTML 页面,其中包含月、日和年的 3 个下拉列表,我想知道是否有办法根据月和年正确填充月下拉列表。

我以前没有在客户端这样做过,但看起来像 jQuery DatePicker 这样的很多控件都在幕后执行此操作。

4

8 回答 8

30

据我所知,没有(整洁的)内置函数。我写过一次:

// 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];
}
于 2011-02-03T01:50:34.400 回答
18

您可以玩日期对象:

var monthStart = new Date(year, month, 1);
var monthEnd = new Date(year, month + 1, 1);
var monthLength = (monthEnd - monthStart) / (1000 * 60 * 60 * 24)

带有对象的算术Date给出了毫秒数。

这甚至适用于 12 月;Date 构造函数通过环绕处理超出范围的参数。

请注意,它month是从零开始的(它必须在0和之间11

于 2011-02-03T01:55:28.360 回答
3

从另一篇文章复制:使用javascript获取指定月份的天数?

//Month is 1 based
function daysInMonth(month,year) {
return new Date(year, month, 0).getDate();
}

//July
daysInMonth(7,2009); //31
//February
daysInMonth(2,2009); //28
daysInMonth(2,2008); //29

@c_harm 的所有功劳,非常棒的解决方案

于 2014-06-30T06:25:06.087 回答
2
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));
于 2011-02-03T04:05:57.417 回答
1

这是一个班轮。假设你说一月=1,二月=2 等等。(正常)这是闰年的例子:

var y = 2012;
var m = 2;
var daysInMonth = new Date(y,m,1,-1).getDate();
于 2013-08-09T12:26:11.153 回答
0

我在我当前的项目中使用这种方法,发现我需要纠正舍入错误。因此,我不得不在我的代码中使用monthLength,而不是使用它:

monthLength.toFixed(0)

例如,如果我有一个存储文本日期字段的对象,它可能如下所示:

obj.month = theMonths[mm - 1] + " " + monthLength.toFixed(0) + ", " + obj.year;
于 2014-02-07T14:19:41.490 回答
0

你实际上可以使用这个:

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

;)

于 2015-03-18T03:11:58.573 回答
-2
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);
             }
         }
     }

 }
于 2016-01-12T11:25:42.373 回答