JS getmonth () the range of date functions is 0-11_ time date

Source: Internet
Author: User
Tags numeric value
When I select the month list item, I get the year value and the month value to calculate the date value of the month 1th and the last day of the month.

The correct code is as follows:
Copy Code code as follows:

<script language= "JavaScript" >
function Seldate (y, M)
{
Output string for the month, 1th
document.form1.startdt.value=y+ "-" +m+ "-1";
Calculates the year (Y), month value (m) of the next month
if (m==12)
{
y++;
M=1;
}
Else
{
m++;
}
Generate date value for 1st of next month
var dt=new Date (Y, m-1, 1); Month Value 0--11
Day difference = 86400000, convert next month 1st to a numeric value, then subtract, last month date value
var n=date.parse (DT);
N-= 86400000;
var dt1=new Date (n);
Output date string for the last day of the month
Document.form1.stopdt.value=dt1.getyear () + "-" + (Dt1.getmonth () +1) + "-" +dt1.getdate ();
}
</script>

I'll start with "var dt=new Date (Y, m-1, 1);" Written "Var dt=new Date (Y, M, 1);". And the last sentence "Dt1.getmonth ()" did not add 1. The result can be imagined, obviously is "2010-2-28", the output is "2010-1-28", and "2010-1-31" Output is "2010-0-31".

Just beginning to feel puzzled, tried, found the error law, found that the month range is "0-11" instead of "1-12".

Hope that the next period of time after the use of JS, this article can remind me.

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.