diff --git a/src/index.js b/src/index.js index 061ade178..5ba730ffd 100644 --- a/src/index.js +++ b/src/index.js @@ -348,7 +348,13 @@ class Dayjs { } diff(input, units, float) { - const unit = Utils.p(units) + let unit + if (typeof input === 'string' && !units) { + unit = Utils.p(input) + input = dayjs() + } else { + unit = Utils.p(units) + } const that = dayjs(input) const zoneDelta = (that.utcOffset() - this.utcOffset()) * C.MILLISECONDS_A_MINUTE const diff = this - that diff --git a/test/display.test.js b/test/display.test.js index ce1f47893..e458d812d 100644 --- a/test/display.test.js +++ b/test/display.test.js @@ -1,8 +1,8 @@ -import moment from 'moment' import MockDate from 'mockdate' +import moment from 'moment' import dayjs from '../src' -import th from '../src/locale/th' import '../src/locale/ja' +import th from '../src/locale/th' beforeEach(() => { MockDate.set(new Date()) @@ -219,6 +219,14 @@ describe('Difference', () => { it('undefined edge case', () => { expect(dayjs().diff(undefined, 'seconds')).toBeDefined() }) + + it('diff with the current time if no date is supplied', () => { + const base = dayjs().add(36, 'hour') + const units = ['seconds', 'minutes', 'hours', 'days', 'weeks', 'months', 'quarters', 'years'] + units.forEach((u) => { + expect(base.diff(u)).toBe(base.diff(dayjs(), u)) + }) + }) }) it('Unix Timestamp (milliseconds)', () => {