JavaScript determines how many days of a certain year the implementation code recommended _ time and date

Source: Internet
Author: User
Tags getdate time and date
The general practice is to determine the number of months, and then decide how many days (generally with switch), if it is February, but also to determine whether the year of choice is a leap years, and then decide whether it is 28 days or 29 days. This is a very regular practice, and also very logical.

However, if it is to achieve the purpose, it is not so troublesome. JS inside of the new date ("Xxxx/xx/xx") the date of the construction method has a beauty, when you pass in the "xxxx/xx/0" (number No. 0), the date is the "XX" month of the first one months of the last day ("XX" month's maximum value is 69, digression), if the incoming "1999/13/0", will get "1998/12/31". And the biggest advantage is when you pass in "xxxx/3/0", will be xxxx year February the last day, it will automatically determine whether it is a leap year to return 28 or 29, do not own judgment, too convenient!! So, we want to choose how many days the choice of year, only need
Copy Code code as follows:

var temp=new Date ("Select year/Select Month +1/0");
Alert (Temp.getdate ());

It's okay, isn't it convenient? Check, you can also use this method.
The following is a getdaysinmonth (year, Month) method written using JS to obtain the number of days of a certain month:
Copy Code code as follows:

function Getdaysinmonth (year,month) {
month = parseint (month,10) +1;
var temp = new Date (year+ "/" +month+ "/0");
return Temp.getdate ();
}

Here is the simple test code:
<script> function Getdaysinmonth (year,month) {month = parseint (month,10) +1; var temp = new Date (year+ "/" +month+ "/0"); return Temp.getdate (); Alert (Getdaysinmonth ("2009", "11")); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

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.