Skip to content

Commit

Permalink
fix: dayjs add inconsistency with weeks
Browse files Browse the repository at this point in the history
  • Loading branch information
github-roushan committed Jan 16, 2025
1 parent 53dbe26 commit 2acb34f
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 10 deletions.
11 changes: 2 additions & 9 deletions src/plugin/duration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,7 @@ class Duration {
}

get(unit) {
let base = this.$ms
const pUnit = prettyUnit(unit)
if (pUnit === 'milliseconds') {
base %= 1000
} else if (pUnit === 'weeks') {
base = roundNumber(base / unitToMS[pUnit])
} else {
base = this.$d[pUnit]
}
const base = this.$d[`${prettyUnit(unit)}`]
return base || 0 // a === 0 will be true on both 0 and -0
}

Expand Down Expand Up @@ -266,6 +258,7 @@ const manipulateDuration = (date, duration, k) =>
.add(duration.minutes() * k, 'm')
.add(duration.seconds() * k, 's')
.add(duration.milliseconds() * k, 'ms')
.add(duration.weeks() * k, 'w')

export default (option, Dayjs, dayjs) => {
$d = dayjs
Expand Down
2 changes: 1 addition & 1 deletion test/plugin/duration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ describe('Days', () => {
})

describe('Weeks', () => {
expect(dayjs.duration(1000000000).weeks()).toBe(1)
expect(dayjs.duration(1000000000).weeks()).toBe(0)
expect(dayjs.duration(1000000000).asWeeks().toFixed(2)).toBe('1.65')
})

Expand Down

0 comments on commit 2acb34f

Please sign in to comment.