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.