Skip to content

Commit f0afbda

Browse files
committed
fix(duration): preserve weeks in toISOString output
Fix issue where weeks were being converted to days in ISO duration strings. Now weeks are preserved as a separate unit in the output, conforming to ISO 8601 standard. Fixes #2859
1 parent 807face commit f0afbda

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

src/plugin/duration/index.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,8 @@ class Duration {
127127
toISOString() {
128128
const Y = getNumberUnitFormat(this.$d.years, 'Y')
129129
const M = getNumberUnitFormat(this.$d.months, 'M')
130-
131-
let days = +this.$d.days || 0
132-
if (this.$d.weeks) {
133-
days += this.$d.weeks * 7
134-
}
135-
136-
const D = getNumberUnitFormat(days, 'D')
130+
const W = getNumberUnitFormat(this.$d.weeks, 'W')
131+
const D = getNumberUnitFormat(this.$d.days, 'D')
137132
const H = getNumberUnitFormat(this.$d.hours, 'H')
138133
const m = getNumberUnitFormat(this.$d.minutes, 'M')
139134

@@ -148,6 +143,7 @@ class Duration {
148143
const negativeMode =
149144
Y.negative ||
150145
M.negative ||
146+
W.negative ||
151147
D.negative ||
152148
H.negative ||
153149
m.negative ||
@@ -156,7 +152,7 @@ class Duration {
156152
const T = H.format || m.format || S.format ? 'T' : ''
157153
const P = negativeMode ? '-' : ''
158154

159-
const result = `${P}P${Y.format}${M.format}${D.format}${T}${H.format}${m.format}${S.format}`
155+
const result = `${P}P${Y.format}${M.format}${W.format}${D.format}${T}${H.format}${m.format}${S.format}`
160156
return result === 'P' || result === '-P' ? 'P0D' : result
161157
}
162158

@@ -261,6 +257,7 @@ class Duration {
261257
const manipulateDuration = (date, duration, k) =>
262258
date.add(duration.years() * k, 'y')
263259
.add(duration.months() * k, 'M')
260+
.add((duration.$d.weeks || 0) * k, 'w')
264261
.add(duration.days() * k, 'd')
265262
.add(duration.hours() * k, 'h')
266263
.add(duration.minutes() * k, 'm')

0 commit comments

Comments
 (0)