Skip to content

Commit f0f5e08

Browse files
authored
Merge pull request #253 from zbinlin/fix-round-to-string-unit
fix: fixes round to string unit
2 parents 0ea55ec + 73bd58c commit f0f5e08

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

src/duration.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,14 @@ export function roundToSingleUnit(duration: Duration, {relativeTo = Date.now()}:
155155
const currentDate = relativeTo.getDate()
156156
if (days >= 27 || (years + months && days)) {
157157
relativeTo.setDate(currentDate + days * sign)
158-
days = 0
159158
months += Math.abs(
160159
relativeTo.getFullYear() >= currentYear
161160
? relativeTo.getMonth() - currentMonth
162161
: relativeTo.getMonth() - currentMonth - 12,
163162
)
163+
if (months) {
164+
days = 0
165+
}
164166
currentMonth = relativeTo.getMonth()
165167
}
166168

test/duration.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ suite('duration', function () {
301301
relativeTo: new Date('2022-01-01T00:00:00Z'),
302302
},
303303
],
304+
['-P27D', '-P1M', {relativeTo: new Date('2023-02-28T00:00:00Z')}],
304305
])
305306
for (const [input, expected, opts] of roundTests) {
306307
test(`roundToSingleUnit(${input}) === ${expected}`, () => {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy