Javascript:计算给定年份的月份天数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4881938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript: calculate number of days in month for a given year
提问by Abe
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 这样的控件在幕后都在做这件事。
回答by Matti Virkkunen
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];
}
回答by SLaks
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; the Date constructor handles out-of-range arguments by wrapping around.
这甚至适用于 12 月;Date 构造函数通过环绕处理超出范围的参数。
Note that month
is zero-based (it must be between 0
and 11
)
请注意,它month
是从零开始的(它必须在0
和之间11
)
回答by David Sánchez
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 的所有功劳,非常好的解决方案
回答by kennebec
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));
回答by Adam
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();
回答by user3018981
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;
回答by reynan
You can actually use this:
你实际上可以使用这个:
var curdate = new Date(); DaysMonth = 32 - new Date(curdate.getYear(), curdate.getMonth(), 32).getDate();
var curdate = new Date(); DaysMonth = 32 - new Date(curdate.getYear(), curdate.getMonth(), 32).getDate();
;)
;)
回答by Shereen Bashar
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);
}
}
}
}