From 7afd8ebe9664fc81efc183263dbcbeb4406db8a2 Mon Sep 17 00:00:00 2001 From: zazzaz <5992643+manre57@users.noreply.github.com> Date: Sat, 1 Aug 2020 20:17:36 +0800 Subject: [PATCH] fix: update monthDiff function to get more accurate results --- src/utils.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/utils.js b/src/utils.js index 7b1712b..6ef4ce6 100644 --- a/src/utils.js +++ b/src/utils.js @@ -16,11 +16,12 @@ const padZoneStr = (instance) => { 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.clone().add(wholeMonthDiff, C.M) + const anchor = a.add(wholeMonthDiff, C.M) const c = b - anchor < 0 - const anchor2 = a.clone().add(wholeMonthDiff + (c ? -1 : 1), C.M) - return Number(-(wholeMonthDiff + ((b - anchor) / (c ? (anchor - anchor2) : + const anchor2 = a.add(wholeMonthDiff + (c ? -1 : 1), C.M) + return +(-(wholeMonthDiff + ((b - anchor) / (c ? (anchor - anchor2) : (anchor2 - anchor)))) || 0) }