Skip to content

Commit 800f6c9

Browse files
committed
fix: Fix .add day/week decimal rouding bug
1 parent 40bea40 commit 800f6c9

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ class Dayjs {
243243
const unit = Utils.p(units)
244244
const instanceFactorySet = (n) => {
245245
const date = new Date(this.$d)
246-
date.setDate(date.getDate() + (n * number))
246+
date.setDate(date.getDate() + Math.round(n * number))
247247
return Utils.w(date, this)
248248
}
249249
if (unit === C.M) {

test/manipulate.test.js

+7
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ it('Add Time days', () => {
6464
expect(dayjs().add('2', 'years').valueOf()).toBe(moment().add('2', 'years').valueOf())
6565
})
6666

67+
it('Add Time with decimal', () => {
68+
expect(dayjs().add(0.4, 'day').valueOf()).toBe(moment().add(0.4, 'day').valueOf())
69+
expect(dayjs().add(0.5, 'day').valueOf()).toBe(moment().add(0.5, 'day').valueOf())
70+
expect(dayjs().add(0.4, 'week').valueOf()).toBe(moment().add(0.4, 'week').valueOf())
71+
expect(dayjs().add(0.5, 'week').valueOf()).toBe(moment().add(0.5, 'week').valueOf())
72+
})
73+
6774
it('Subtract Time days', () => {
6875
expect(dayjs().subtract(1, 'days').valueOf()).toBe(moment().subtract(1, 'days').valueOf())
6976
})

0 commit comments

Comments
 (0)