简体   繁体   English

Javascript:计算给定年份的月数

[英]Javascript: calculate number of days in month for a given year

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. 我有一个HTML页面,包含月份,日期和年份的3个下拉列表,我想知道是否有办法根据月份和年份正确填充月份下拉列表。

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. 我之前没有在客户端做过这个,但看起来像jQuery DatePicker这样的很多控件都是在幕后做的。

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];
}

You can play with date objects: 您可以使用日期对象:

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

Arithmetic with Date objects gives a number of milliseconds. 使用Date对象的算术给出了毫秒数。

This will even work for December; 这甚至适用于12月; the Date constructor handles out-of-range arguments by wrapping around. Date构造函数通过环绕处理超出范围的参数。

Note that month is zero-based (it must be between 0 and 11 ) 请注意, month从零开始(必须介于011之间)

Copy from another post: Get number days in a specified month using javascript? 从另一篇文章复制: 使用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

All the credits to @c_harm, really great solution 所有@c_harm的学分,真的很棒的解决方案

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));

Here's the one liner. 这是一个班轮。 Assuming you are saying January=1, February=2 etc..(being normal) Here's the leap year example: 假设您说的是1月= 1,2月= 2等...(正常)这是闰年示例:

var y = 2012;
var m = 2;
var daysInMonth = new Date(y,m,1,-1).getDate();

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,而是使用它:

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;

You can actually use this: 你实际上可以使用这个:

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

;) ;)

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);
             }
         }
     }

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM