Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 3 additions & 2 deletions 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 Expand Up @@ -294,7 +294,8 @@ describe('Format', () => {
.add(16, 'days')
.add(10, 'months')
.add(22, 'years')
expect(d.format()).toBe('0022-10-16T13:35:15')
.add(1, 'weeks')
expect(d.format()).toBe('0022-10-23T13:35:15')
})

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