• background

    • Distance is launched last day, in the evening after work when we test the little sister finally find that I’m here, date plugin problem, suddenly a tiger body, feel the distance from work time again far step, this plugin from other projects directly bring them here, has been running for a long time online, and how will there be any problem.
    • First let the test sister mo worry, and then understand the problem, the original plug-in initialization is in March, click next January when the direct jump to May, initially listen to a face meng force, and this magical bug, as expected, bug, there are many.
  • screening

    • We started an investigation and finally found the problem of new Date(). Since the default Date is March 31, the time of new Date() is 2021-3-31, and the problem lies on this 31.
  • The principle of

    • Create a JavaScript Date instance that renders a moment in time. The Date object is based on the Unix Time Stamp, the number of milliseconds since January 1, 1970 (UTC).

    • The Date() constructor has four basic forms

      • new Date(a);// If no arguments are provided, the newly created Date object represents the Date and time of the instantiation moment.
        new Date(value);  // A Unix Time Stamp, which is an integer value representing the number of milliseconds since 00:00:00 UTC (the Unix Epoch) on January 1, 1970, ignoring leap seconds. Note that most Unix timestamp features are only accurate to the nearest second
        new Date(dateString); // A string value representing a date. The string should be recognized by the correct date.parse () method
        new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);// When at least a year and month are provided, this form of Date() returns each member of the Date object from the following arguments. Members not provided will use the lowest possible value (1 for dates, 0 for others).
        // The parameter monthIndex is calculated from "0", which means January is "0" and December is "11".
        Copy the code
    • New Date(year, monthIndex, day) New Date(2021,3,31) : Sat May 01 2021 00:00:00 GMT+0800 (Chinese standard time)

    • Note that the parameter monthIndex is calculated from “0”, which means “0” for January and “11” for December.

    • The first thing we need to know is that when we pass in a format like new Date(2021,3,31), the month is automatically incremented by one to get the real month, i.e. march is passed in and April is passed in.

    • ** Note: ** When Date is called as a constructor and multiple arguments are passed, adjacent values will be adjusted if the value is greater than a reasonable range (such as 13 months or 70 minutes). For example, new Date(2013, 13, 1) equals new Date(2014, 1, 1), both representing the Date 2014-02-01 (note that the month starts from 0). Other values are similar, new Date(2013, 2, 1, 0, 70) is equal to new Date(2013, 2, 1, 1, 10), both representing the same time: 2013-03-01T01:10:00. (MDN)

    • Therefore, when the new Date(2021,3,31), since march is 31 days and April does not have 31 days, the value is greater than the reasonable range, and the adjacent value is adjusted to May 1.

  • The solution

    • The easiest way to change the parameter format is to change new Date(YYYY,MM,DD) to New Date(YYYY-MM-DD), but this format has changed too much in my project code and is not suitable for my current situation.

    • Since it is related to the days and months of the month, solve the problem from the month and days of the current month, namely the following solution

      • var currentDay; // Number of days in the current month
        var currentMonthDays = new Date(year, month, 0).getDate(); // Get the number of days in the current month
        var nextMonthDays = new Date(year, month + 1.0).getDate();  // Get the number of days in January
        var days = currentDay > nextMonthDays? nextMonthDays: currentDay; // If the number of days in the current month is greater than that in the next month, days is the number of days in the next month; otherwise, days is the number of days in the current month
        new Date(that.year, that.month, days);
        // Can solve this problem
        Copy the code

I am a scholar, a small spray in the front spray.