Skip to content

Commit f0fc12a

Browse files
authored
fix: Duration plugin supports parse ISO string with week (W) (#950)
1 parent 81d4740 commit f0fc12a

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/plugin/duration/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Duration {
4848
const d = input.match(durationRegex)
4949
if (d) {
5050
[,,
51-
this.$d.years, this.$d.months,,
51+
this.$d.years, this.$d.months, this.$d.weeks,
5252
this.$d.days, this.$d.hours, this.$d.minutes, this.$d.seconds] = d
5353
this.calMilliseconds()
5454
return this
@@ -83,7 +83,7 @@ class Duration {
8383
toISOString() {
8484
const Y = this.$d.years ? `${this.$d.years}Y` : ''
8585
const M = this.$d.months ? `${this.$d.months}M` : ''
86-
let days = this.$d.days || 0
86+
let days = +this.$d.days || 0
8787
if (this.$d.weeks) {
8888
days += this.$d.weeks * 7
8989
}

test/plugin/duration.test.js

+6
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ describe('Parse ISO string', () => {
6262
it('Part ISO string', () => {
6363
expect(dayjs.duration('PT2777H46M40S').toISOString()).toBe('PT2777H46M40S')
6464
})
65+
it('ISO string with week', () => {
66+
const d = dayjs.duration('P2M3W4D')
67+
expect(d.toISOString()).toBe('P2M25D')
68+
expect(d.asDays()).toBe(85) // moment 85, count 2M as 61 days
69+
expect(d.asWeeks()).toBe(12.142857142857142) // moment 12.285714285714286
70+
})
6571
it('Invalid ISO string', () => {
6672
expect(dayjs.duration('Invalid').toISOString()).toBe('P0D')
6773
})

0 commit comments

Comments
 (0)