Javascript:计算给定年份的月份天数 [英] Javascript: calculate number of days in month for a given year
问题描述
我有一个HTML页面,这个月份和日期都有3个下拉列表,我想知道是否有办法根据月份和年份正确填写月份下降。
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.
我以前没有在客户端这样做,但它看起来像很多控件,如jQuery DatePicker在幕后。
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.
推荐答案
您可以使用日期对象:
var monthStart = new Date(year, month, 1);
var monthEnd = new Date(year, month + 1, 1);
var monthLength = (monthEnd - monthStart) / (1000 * 60 * 60 * 24)
使用 Date 对象的算术可以产生几毫秒。
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.
请注意, month
是基于零(它必须介于 0
和 11
之间)
Note that month
is zero-based (it must be between 0
and 11
)
这篇关于Javascript:计算给定年份的月份天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!