Skip to content

Commit 2acb34f

Browse files
fix: dayjs add inconsistency with weeks
1 parent 53dbe26 commit 2acb34f

File tree

2 files changed

+3
-10
lines changed

2 files changed

+3
-10
lines changed

src/plugin/duration/index.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,7 @@ class Duration {
190190
}
191191

192192
get(unit) {
193-
let base = this.$ms
194-
const pUnit = prettyUnit(unit)
195-
if (pUnit === 'milliseconds') {
196-
base %= 1000
197-
} else if (pUnit === 'weeks') {
198-
base = roundNumber(base / unitToMS[pUnit])
199-
} else {
200-
base = this.$d[pUnit]
201-
}
193+
const base = this.$d[`${prettyUnit(unit)}`]
202194
return base || 0 // a === 0 will be true on both 0 and -0
203195
}
204196

@@ -266,6 +258,7 @@ const manipulateDuration = (date, duration, k) =>
266258
.add(duration.minutes() * k, 'm')
267259
.add(duration.seconds() * k, 's')
268260
.add(duration.milliseconds() * k, 'ms')
261+
.add(duration.weeks() * k, 'w')
269262

270263
export default (option, Dayjs, dayjs) => {
271264
$d = dayjs

test/plugin/duration.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ describe('Days', () => {
261261
})
262262

263263
describe('Weeks', () => {
264-
expect(dayjs.duration(1000000000).weeks()).toBe(1)
264+
expect(dayjs.duration(1000000000).weeks()).toBe(0)
265265
expect(dayjs.duration(1000000000).asWeeks().toFixed(2)).toBe('1.65')
266266
})
267267

0 commit comments

Comments
 (0)