In the past, when I was writing a webpage, I often encountered the problem of selecting a date, which is actually
Determine the number of days of a month in a year . The general practice is to first determine the number of months, and then determine the number of days (usually with a switch), if it is February, You have to determine whether the selected year is a leap year, then decide whether it is 28 days or 29 days. This is a very regular practice and logical.
However, if it is for the purpose, it is not so troublesome.JSLi
New
The construction method of date ("xxxx/XX/xx") has a wonderful effect. When you input "xxxx/XX/0" (No. 0, the date obtained is the first one in the month of "XX ".
The last day of the month (the maximum value of "XX" month is 69. If you input "", you will get ". The biggest benefit is that when you pass in "xxxx/3/0", you will get the last day of January 1, February, xxxx. It will automatically determine whether the year is a leap year and return 28 or 29. You do not have to judge it by yourself, it's so convenient !! Therefore, if we want to select the number of days in a month, we only need
VaR temp = new date ("Select Year/select month + 1/0 ");
Alert (temp. getdate ());
That's all. Is it very convenient? This method can also be used for verification.
UseJSCompile the getdaysinmonth (year, month) method to obtain the number of days of a month in a year:
function getdaysinmonth (year, month) {
month = parseint (month, 10) + 1;
var temp = new date (year + "/" + month + "/0");
return temp. getdate ();
}