diff --git a/src/plugin/utc/index.js b/src/plugin/utc/index.js index 3030f2b8a..8b9147b63 100644 --- a/src/plugin/utc/index.js +++ b/src/plugin/utc/index.js @@ -104,4 +104,10 @@ export default (option, Dayjs, dayjs) => { } return oldToDate.call(this) } + const oldDiff = proto.diff + proto.diff = function (input, units, float) { + const localThis = this.local() + const localInput = dayjs(input).local() + return oldDiff.call(localThis, localInput, units, float) + } } diff --git a/test/plugin/utc.test.js b/test/plugin/utc.test.js index dc290de1e..d8fd7da27 100644 --- a/test/plugin/utc.test.js +++ b/test/plugin/utc.test.js @@ -219,3 +219,26 @@ describe('UTC Offset', () => { expect(dayjs().utc().utcOffset()).toBe(moment().utc().utcOffset()) }) }) + +describe('Diff', () => { + const d1 = '2021-06-07' + const d2 = '2021-06-06' + it('utc.diff(utc)', () => { + [dayjs, moment].forEach((_) => { + expect(_.utc(d1).diff(_.utc(d2), 'days')).toBe(1) + expect(_.utc(d1).diff(_.utc(d2), 'm')).toBe(1440) + }) + }) + it('local.diff(utc)', () => { + expect(dayjs(d1).diff(dayjs.utc(d2), 'days')) + .toBe(moment(d1).diff(moment.utc(d2), 'days')) + expect(dayjs(d1).diff(dayjs.utc(d2), 'm')) + .toBe(moment(d1).diff(moment.utc(d2), 'm')) + }) + it('utc.diff(local)', () => { + expect(dayjs.utc(d1).diff(d2, 'days')) + .toBe(moment.utc(d1).diff(d2, 'days')) + expect(dayjs.utc(d1).diff(d2, 'm')) + .toBe(moment.utc(d1).diff(d2, 'm')) + }) +})