Skip to content

Commit

Permalink
fix: duration plugin - MILLISECONDS_A_MONTH const calculation (#2362)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjaynishad committed Jul 9, 2023
1 parent 890a17a commit f0a0b54
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/plugin/duration/index.js
Expand Up @@ -8,7 +8,7 @@ import {
} from '../../constant'

const MILLISECONDS_A_YEAR = MILLISECONDS_A_DAY * 365
const MILLISECONDS_A_MONTH = MILLISECONDS_A_DAY * 30
const MILLISECONDS_A_MONTH = MILLISECONDS_A_YEAR / 12

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)?)?$/

Expand Down
5 changes: 3 additions & 2 deletions test/plugin/duration.test.js
Expand Up @@ -92,8 +92,9 @@ describe('Parse ISO string', () => {
it('ISO string with week', () => {
const d = dayjs.duration('P2M3W4D')
expect(d.toISOString()).toBe('P2M25D')
expect(d.asDays()).toBe(85) // moment 85, count 2M as 61 days
expect(d.asWeeks()).toBe(12.142857142857142) // moment 12.285714285714286
expect(d.asDays()).toBe(85.83333333333333) // moment 86, count 2M as 61 days
expect(d.asWeeks()).toBe(12.261904761904763) // moment 12.285714285714286
expect(d.asMonths()).toBe(2.8219178082191783) // moment 2.8213721020965523
})
it('Invalid ISO string', () => {
expect(dayjs.duration('Invalid').toISOString()).toBe('P0D')
Expand Down

0 comments on commit f0a0b54

Please sign in to comment.