JavaScript to get the last day of the month code

Source: Internet
Author: User

The simplest code

The code is as follows Copy Code

<script type= "Text/javascript" >
<!--
document.write (New Date (2009, 4, 1)); The 1th Day of May 2009 (the January value is 0), May 1, 2009
document.write (New Date (2009, 5, 0)); The No. 0 Day of June 2009, the last day of May 2009
-->
</script>


Another way of writing

The code is as follows Copy Code

<title> title page </title>
<script language= "JavaScript" >
function Getlastday (year,month)
{
var new_year = year; Take the current year
var new_month = month++;//Remove the first day of one months for easy calculation (last day is not fixed)
if (month>12)//If the current is greater than December, the year is transferred to the next year
{
New_month-=12; Month minus
new_year++; Year increase
}
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 ()//Get 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>

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.