JS gets the maximum number of days in a month and the last day to get the month

Source: Internet
Author: User

The code is as follows:
<title> title page </title>
<script language=javascript>
function Getlastday (year,month)
{
var new_year = year; Take the current year
var new_month = month++;//removed one month first day, convenient calculation (last day not fixed)
if (month>12)//If the current is greater than December, the year goes to the next year
{
New_month-=12; Month minus
new_year++; Year increases
}
var new_date = new Date (new_year,new_month,1); Take the first day of the current month
Return (new Date (New_date.gettime () -1000*60*60*24)). GETDATE ();//Gets the date of the last day of the month
}
</script>
<body>
<input Id=button1 Type=button value= Take the last day of May 2007 Onclick=alert (Getlastday (2007,5))/>
</body>



JS to one month maximum number of days

JS inside the new date (XXXX/XX/XX) This dates to construct method has a beauty,

When you pass in the ground is xxxx/xx/0 (No. 0), the date of the land is xx month before the last day of one months (the maximum value of XX month is 69, off-topic),

When you pass in the ground is XXXX/XX/1 (1th), the date of the land is xx months after the first day of one months (self understanding)

If passed in 1999/13/0, will be to 1998/12/31. And the biggest advantage is that when you pass into the xxxx/3/0, will be the last day of XXXX year February, it will automatically determine whether it is a leap year to return 28 or 29, without their own judgment,

So, we want to choose the year to select the number of heaven and earth words, only need

var temp=new date (Select year/select Month +1/0);

return temp.getdate ()//Maximum days

Check the ground, you can also use this method.

Here is how to use JS to get the number of getdaysinmonth (year, Month) method for a certain month:

The code is as follows:
function Getdaysinmonth (year,month) {
month = parseint (month,10) +1;
var temp = new Date (year+/+month+/0);
return Temp.getdate ();
}

JS gets the maximum number of days in a month and the last day to get the month

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.