Skip to content

Commit

Permalink
fix: support dayjs.add(Duration), dayjs.subtract(Duration) (#1099)
Browse files Browse the repository at this point in the history
  • Loading branch information
stebogit authored Oct 2, 2020
1 parent 16937d1 commit b1a0294
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/plugin/duration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,13 @@ export default (option, Dayjs, dayjs) => {
return wrapper(input, { $l }, unit)
}
dayjs.isDuration = isDuration

const oldAdd = Dayjs.prototype.add
Dayjs.prototype.add = function (addition, units) {
if (isDuration(addition)) {
addition = addition.asMilliseconds()
units = 'ms'
}
return oldAdd.bind(this)(addition, units)
}
}
6 changes: 6 additions & 0 deletions test/plugin/duration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ describe('Add', () => {
expect(a.add({ days: 5 }).days()).toBe(6)
})

describe('Add duration', () => {
const a = dayjs('2020-10-01')
const days = dayjs.duration(2, 'days')
expect(a.add(days).format('YYYY-MM-DD')).toBe('2020-10-03')
})

describe('Subtract', () => {
const a = dayjs.duration(3, 'days')
const b = dayjs.duration(2, 'days')
Expand Down

0 comments on commit b1a0294

Please sign in to comment.