Skip to content

Commit c1d37ff

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

File tree

2 files changed

+5
-11
lines changed

2 files changed

+5
-11
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: 3 additions & 2 deletions
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

@@ -294,7 +294,8 @@ describe('Format', () => {
294294
.add(16, 'days')
295295
.add(10, 'months')
296296
.add(22, 'years')
297-
expect(d.format()).toBe('0022-10-16T13:35:15')
297+
.add(1, 'weeks')
298+
expect(d.format()).toBe('0022-10-23T13:35:15')
298299
})
299300

300301
test('with formatStr for all tokens', () => {

0 commit comments

Comments
 (0)