The value range of the JS getMonth () Date function is 0-11.

Source: Internet
Author: User

After selecting the month list item, I get the Year Value and month value, and calculate the date value of the first day of the month and the last day of the month.

The correct code is as follows:
Copy codeThe Code is as follows:
<Script language = "javascript">
Function selDate (y, m)
{
// Output the string No. 1 of the current month
Document. form1.startDT. value = y + "-" + m + "-1 ";
// Calculate the year (y) and month (m) of the next month)
If (m = 12)
{
Y ++;
M = 1;
}
Else
{
M ++;
}
// Generate the Date value of January 1, 1st day of next month
Var dt = new Date (y m-1, 1); // month value: 0--11
// The one-day difference is 86400000, which converts January 1, 1st day of next month to a value and then subtract it. The value of Date on the last day of the previous month is obtained.
Var n = Date. parse (dt );
N-= 86400000;
Var dt1 = new Date (n );
// Output the date string of the last day of the current month
Document. form1.stopDT. value = dt1.getYear () + "-" + (dt1.getMonth () + 1) + "-" + dt1.getDate ();
}
</Script>

At first, I wrote "var dt = new Date (y, S-1, 1);" as "var dt = new Date (y, m, 1 );". The last sentence "dt1.getMonth ()" does not add 1. The result is clearly "", but the output is "", while the output is ".

At the beginning, I felt inexplicable. I tried multiple times and found the error rule. I found that the month value range is "0-11" rather than "1-12 ".

I hope this article will remind me that JS will not be used in the next time.

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.