In order to better speak the contents of this section, if a prompt JS processing tips date, want to get the last day of last month, only you need to set the parameter to 0. SetDate.
var endDate = new new a Date ();
// last day of the month
endDate.setDate ( 0 );
Then began the text:
Use JS get the last day, we usually get the idea first day of the first month of the current month, and then subtract a day is the last day of the current month, so naturally we will use the following code:
var DATE = new new a Date ();
Date.setMonth (date.getMonth () + . 1 );
// set the date to No. 0, No. 0 indicates the day before. 1
the let lastDay = Date.setDate ( 0 );
Console .log ( 'last day:' + new new a Date (lastDay) .toLocaleString ())
I write this date 2019.08.21
, the print results are as follows:
Last day: 2019/8/31 4:10:43 pm
Such an algorithm that there is no bug, we are not sure, so in order to restore the site, from now we 2019.01.31
one by one to try
var DATE = new new a Date ( 2019 , 0 , 31 is ); // 0 for January
Date.setMonth (date.getMonth () + . 1 );
// set the date to No. 0, No. 0 indicates the day before. 1
the let lastDay = Date.setDate ( 0 );
Console .log ( 'last day:' + new new a Date (lastDay) .toLocaleString ())
print:
Last day: 2019/2/28 12:00:00 AM
We can see that we put 1月31号
back a month dial in forward minus one day deserve that 1月31号
, actually get is 2月28号
, so we have to write code is a bug.
This is how fat thing?
We can list the month:
January 1 ....... 28 29 30 31
February 1 ....... 28
March 1 28 29 30 31 .......
We pass month + 1
, will be understood to be in JS:
The number of days of the month of the current date +
So in the example is date.setMonth(date.getMonth() + 1)
equivalent to 1月31号 + 31天
, according to the list above, you can see the results of a3月3号
So do not think that is the month + 1 month plus 1, it means that the current date plus the number of days of the month .
How to ensure that not more than one month multihop it?
Just be sure month + 1
not before more than 28
numbers on the line, then how to do it?
Projections:
31 + 31 Huichao
30 + 31 Huichao
29 + 31 Huichao
No. 31 or No. 28 + 28 just
So in the secret 31
number to call back 28
number on the line, so pay more on the line following sentence:
date.setDate(28)
Complete code:
var DATE = new new a Date ( 2019 , 0 , 31 is ); // 0 for January
Date.setDate ( 28 )
Date.setMonth (date.getMonth () + 1 );
// set the date to 0, 1, 0 represents the day before
the let lastDay = Date.setDate ( 0 );
Console .log ( 'last day:' + new new a Date (lastDay) .toLocaleString ())
operation result:
Last day: 2019/1/31 12:00:00 AM
After the code is deployed may exist BUG can not know in real time, and afterwards in order to solve these BUG, we spent a lot of time debugging log, here for everyone to recommend a way BUG easy to use monitoring tools Fundebug.
communicate with
Dry series are summarized below, feel good point of a Star, please add the group to learn from each other.
https://github.com/qq449245884/xiaozhi
I am a little wisdom, public numbers "big move in the world," the author, to keep learning enthusiasts of front-end technology. I would often share their learned the dry look , the way in advanced, encourage each other!
No public attention, background replies welfare , you can see the benefits, you know.