From 40ab6d9a53e8047cfca63c611c25dd045372d021 Mon Sep 17 00:00:00 2001 From: iamkun Date: Mon, 31 Aug 2020 10:59:16 +0800 Subject: [PATCH] fix: Fix BadMutable plugin bug in .diff (#1023) --- src/utils.js | 4 ++-- test/plugin/badMutable.test.js | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/utils.js b/src/utils.js index ae81679f8..324bc5e11 100644 --- a/src/utils.js +++ b/src/utils.js @@ -18,9 +18,9 @@ const monthDiff = (a, b) => { // function from moment.js in order to keep the same result if (a.date() < b.date()) return -monthDiff(b, a) const wholeMonthDiff = ((b.year() - a.year()) * 12) + (b.month() - a.month()) - const anchor = a.add(wholeMonthDiff, C.M) + const anchor = a.clone().add(wholeMonthDiff, C.M) const c = b - anchor < 0 - const anchor2 = a.add(wholeMonthDiff + (c ? -1 : 1), C.M) + const anchor2 = a.clone().add(wholeMonthDiff + (c ? -1 : 1), C.M) return +(-(wholeMonthDiff + ((b - anchor) / (c ? (anchor - anchor2) : (anchor2 - anchor)))) || 0) } diff --git a/test/plugin/badMutable.test.js b/test/plugin/badMutable.test.js index fb4dc723e..bfd351520 100644 --- a/test/plugin/badMutable.test.js +++ b/test/plugin/badMutable.test.js @@ -164,6 +164,16 @@ it('Locale', () => { expect(d.format(format)).toBe(m.format(format)) }) +it('Diff', () => { + const d = dayjs() + const m = moment() + const unit = 'year' + const d2 = d.clone().add(1, unit) + const m2 = m.clone().add(1, unit) + expect(d.diff(d2, unit)).toBe(-1) + expect(m.diff(m2, unit)).toBe(-1) +}) + it('isAfter isBefore isSame', () => { const d = dayjs() const format = dayjs().format()