The problem

Today, I have a business logic to determine that the time range of choices should not exceed 3 months. This kind of routine comparison can be easily done with the diff method of moment.js.

moment('2020-3-30').diff(moment('2020-7-01'), 'months')
Copy the code

Console look at the result: -3.033333333333, good, 3 months, steady ~ is this negative a little uncomfortable, change the start and end time of the position

moment('2020-7-01').diff(moment('2020-3-30'), 'months')
Copy the code

3.064516129032258 = 3.064516129032258 = 3.064516129032258 Moment is stupid? No, I must be stupid…

Train of thought

It doesn’t, look at the document: the diff () | Moment. Js document (momentjs. Cn)

Emmm, there seems to be nothing special, but there is a small line that attracts me: See more discussion on the month and year Diffs here Looks like there is Beef here, even if it can’t be solved, I will also go in to make fun of it, haha

This elder brother, with my problem can’t say appearance match heart and soul apart, can only say one hair is same, original also have with me same of 2 fool (?? Why do you say that about me?) Let me see how the following gods teach him how to be a man…

At the bottom, Gibson posted this link: Difference should always return a “top-heavy balanced” duration with larger-first order of operations · Issue #993 · tc39/proposal-temporal (github.com)

One of the comments said:

The algorithm Java uses seems to be something like this:

  1. Start with the smaller value: 2020-01-31
  2. Find the number of whole months that can be added to it without going past 2020-03-30. That’s one month.
  3. Add one month to 2020-01-31 and constrain that result to a valid date, e.g. 2020-02-29
  4. Now find the number of whole days that can be added without going past the end. That’s 30 days.
  5. Return P1M30D

After reading this paragraph, IT suddenly dawned on me. Taking the actual case we met today, I would like to explain how the principle he explained was realized:

  • The diff algorithm adds or subtracts each month until it can’t be subtracted, and then looks at the percentage of the remaining days compared to that month
  • For example, if 07-01 is calculated from zero, minus 3 months, it is the zero of 04-01, and there are still two days left from 3-31 and 3-30 to zero of 3-30. There are 31 days in March, so 2/31=0.0645, which is 3.064516129032258
  • The second kind of case, it has been added to 03-30 of the zero start to calculate, to 06-30 of zero can not add a whole month, then from 7-01 of zero 1 day of time, There are 30 days in June, so the odd is 1/30=0.333333333… , add up to -3.033333333333…

conclusion

So, my moment. Js diff method in comparison with day/month/year such special particle size of the unit, will be priority according to the particle size to deduct the rest of the fractional part, is based on the sub level of granularity that year/month/day for reference according to the ratio to calculate, this is the value of A than B and B than the value of A different situation. Although generally speaking, a little more or less of this value does not matter, after all, we are looking for their own granularity to compare, but this principle can be understood, it is also a “learned” harvest, hey hey

I’m Data Rios