Skip to content

Commit f0a0b54

Browse files
authored
fix: duration plugin - MILLISECONDS_A_MONTH const calculation (#2362)
1 parent 890a17a commit f0a0b54

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

src/plugin/duration/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from '../../constant'
99

1010
const MILLISECONDS_A_YEAR = MILLISECONDS_A_DAY * 365
11-
const MILLISECONDS_A_MONTH = MILLISECONDS_A_DAY * 30
11+
const MILLISECONDS_A_MONTH = MILLISECONDS_A_YEAR / 12
1212

1313
const durationRegex = /^(-|\+)?P(?:([-+]?[0-9,.]*)Y)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)W)?(?:([-+]?[0-9,.]*)D)?(?:T(?:([-+]?[0-9,.]*)H)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)S)?)?$/
1414

test/plugin/duration.test.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,9 @@ describe('Parse ISO string', () => {
9292
it('ISO string with week', () => {
9393
const d = dayjs.duration('P2M3W4D')
9494
expect(d.toISOString()).toBe('P2M25D')
95-
expect(d.asDays()).toBe(85) // moment 85, count 2M as 61 days
96-
expect(d.asWeeks()).toBe(12.142857142857142) // moment 12.285714285714286
95+
expect(d.asDays()).toBe(85.83333333333333) // moment 86, count 2M as 61 days
96+
expect(d.asWeeks()).toBe(12.261904761904763) // moment 12.285714285714286
97+
expect(d.asMonths()).toBe(2.8219178082191783) // moment 2.8213721020965523
9798
})
9899
it('Invalid ISO string', () => {
99100
expect(dayjs.duration('Invalid').toISOString()).toBe('P0D')

0 commit comments

Comments
 (0)