Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions test/display.test.js
Original file line number Diff line number Diff line change
@@ -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())
Expand Down Expand Up @@ -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)', () => {
Expand Down