JS determines the number of days in a month of a year

Source: Internet
Author: User
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 ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.