SoFunction
Updated on 2025-04-10

Get the last day of the current month (quarter/year) (set-related operations and applications)

Today I will only talk about setFullYear, setMonth, and setDate, because today's application only involves these three.

As the name suggests, these three methods set year, month and day respectively. The applications I have learned about before, such as when setting the target time point in the "Countdown", will be used. If you can't remember the others for the time being, you will make up for it again afterwards.

Let's talk about "Get the last day of the current month (quarter/year)Usage of ”

You may have thought that the number of days in each month should not be determined, there are 28, 29 (february of leap year), 30, and 31. You can’t calculate what month it is now, and then go to arr or map (json) to find the corresponding date. Of course, this is also a method, which can achieve the effect (the leap year must also be considered).

So, if it is not so cumbersome, how can you do it?

In fact, Date will automatically handle the special situations of days per month, including leap years, etc., so we don’t need to worry about this at all.

I have used an usage here, and I guess you may have used it too, (0). That's right, you read it right, it's 0. (Hey, this is not month, don't add 1, are you sure you don't want to use setDate(1)?)

0 is a day that does not exist. After (0), this day does not exist, or it is set the day before the 1st. Then, the day before the 1st is naturally the last day of the previous month

var date = new Date();
('Today is', () + 1, ());
(0);
('The last day of last month was', () + 1, ());

Run the above code on the console and you will get the last day of last month.

Continue, what we want is the last day of this month, how to break it

First set the month to the next month, and then get the last day of the month:

(() + 1);
(0);

date is the corresponding date object on the last day of this month.

At this point, simply obtaining the last day of the month has been completed. Changing the setMonth can allow you to obtain the last day of any month.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Dividing line ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Next, let’s talk about getting the date of the last day of the year.

Similarly, we just need to set it until next year, then set it to October (January), and then set it to 0th day, which will become 2017-01-00, which is the day before New Year's Day, of course, it is 2016-12-31.

(() + 1); // Set until next year(0); // Next year's October, which corresponds to January, exists, not non-existent zero.(0); // Next year's0day

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Then, it's the last day of getting the current quarter again.

There is no concept of quarter in Date (if there is, please tell me in time and let me know more about the knowledge points), so first we need to judge which quarter the current month is in.

var m = ();
var quarter = (m / 3) + 1; // Start from 1 to 4var qLastMonth = quarter * 3;

Among them, quarter is the quarter, and qLastMonth is the last month of this quarter. // From 1 to 12, you can subtract 1 from 0 to 11 in that year.

Since the last month of this quarter is obtained, then use the above method to obtain the last day of any month to deal with it.

(qLastMonth);
(0);

At this time, date is the date object on the last day of the current quarter. Of course, you can get the last day of any quarter by doing an expansion. Just set it in the first step (hello, what are you doing? It's a long way around)

To get the last day of any quarter, such as the second quarter, it only needs to. . .

(6); // 4 || 5 || 6 are fine(0);

Finally, come to the practical application encountered in a project

It's purple:

  1. Get the last day of the quarter; when today is the last day of the current quarter cuts greater than the 15th (such as June 20th), return to the last day of the next quarter.
  2. Get the last day of the year; when today is greater than December 15, return to the last day of next year.

The implementation code at the beginning is as follows:

/**
  * Get the date object for the last day of the current month/current year/current quarter
  * @param {String} type type selection: month/m, year/y, quarter/q
  * @return {object} The last day's date object, currently only available to the day
  */
var SERVER_TIME = () / 1000; // Server time, local time is used instead offunction getMaxDate(type) {
 var date = new Date(SERVER_TIME * 1000);
 var m = ();
 var y = ();
 var d = ();
 var today = ();
 (['SERVERTIME: ',y,'-',m+1,'-',today,' Week',d].join(''));
 switch (type) {
 case 'm':
 case 'month':
  (m + 1);
  (0);
  break;
 case 'y':
 case 'year':
  if (m == 11 && today >= 15) {
  (y + 2);
  } else {
  (y + 1);
  }
  (0);
  (0);
  break;
 case 'q':
 case 'quarter':
  var qLastMonth = ((m / 3) + 1) * 3;
  (qLastMonth);
  (0);
  if (m === () && today >= 15) {
  (qLastMonth + 3);
  (0);
  }
  break;
 default:
  date = null;
 }
 return date;
}

Then, there is a big pit in this code, which may not be encountered normally. It will come out before the end of the year (fortunately it is not a once-in-a-thousand-year encounter like a thousand-year insect)

When selecting quarterly, 36 lines may cause problems with the code blocks of the following 38 lines.

For example, in the second quarter, there is no problem. The date of 36 lines is set to 30th, that is, June 30th; if the 38 lines of code logic is met, the month from 39-40 lines is set to 30th September 30th, there is no problem, it is perfect ~

However, if it is the first quarter, the 36-line setting date is March 31; if the 38-line code logic is met, the 39-40-line setting month is June 31, Asi, June does not have 31, then the date you get at this time will be ~~~~~ That's right, on July 1, you will dig a hole.

So, change the code

function getMaxDate(type) {
 var date = new Date(SERVER_TIME * 1000);
 var m = ();
 var y = ();
 var d = ();
 var today = ();
 (['SERVERTIME: ', y, '-', m + 1, '-', today, ' Week', d].join(''));
 switch (type) {
 case 'm':
 case 'month':
  (m + 1);
  (0);
  break;
 case 'y':
 case 'year':
  if (m == 11 && today >= 15) {
  (y + 2);
  } else {
  (y + 1);
  }
  (0);
  (0);
  break;
 case 'q':
 case 'quarter':
  var qLastMonth = ((m / 3) + 1) * 3;
  if (m === () && today >= 15) {
  (qLastMonth + 3);
  } else {
  (qLastMonth);
  }
  (0); // Get the date of the last day of the current quarter  break;
 default:
  date = null;
 }
 return date;
}

Here we get a note: The relevant judgment of the month must be made before the day setting, and do not operate the month after the date setting; or you set the date to any number between 1-28, which means you will not go to the next month.

The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!