iamkun / iamkun/dayjs

Inconsistent Duration Units

Open
#1,491 2 comments 5 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
48.7k
Forks
2.5k
PR merge metrics
No merged PRs in 30d

Description

Currently there seems to be some inconsistency and duplication of units when parsing month/week durations:

**Current Behavior**

```javascript
dayjs.duration('P1M').months(); // "1"
dayjs.duration('P1M').weeks(); // 4

dayjs.duration('P4W').weeks(); // 4
dayjs.duration('P4W').months(); // undefined

dayjs.duration('P1M4W').months(); // "1"
dayjs.duration('P1M4W').weeks(); // 8
```

Months are always converted into 4 weeks (additionally to the `month` unit also being set).

This behavior is not congruent with other units:

```javascript
dayjs.duration('P1Y').years(); // "1"
dayjs.duration('P1Y').months(); // undefined
dayjs.duration('P1Y').weeks(); // 52

dayjs.duration('P12M').years(); // undefined
dayjs.duration('P12M').months(); // "12"
dayjs.duration('P12M').weeks(); // 51
```

**Expected Behavior**

```javascript
dayjs.duration('P1M').months(); // 1
dayjs.duration('P1M').weeks(); // undefined

dayjs.duration('P4W').weeks(); // 4
dayjs.duration('P4W').months(); // undefined

dayjs.duration('P1M4W').months(); // 1
dayjs.duration('P1M4W').weeks(); // 4
```

**Information**
- Day.js Version: 1.10.4

Contributor guide

Open the contributing guide

Research direction

Start by locating the duration parsing and unit-conversion entry points, then inspect the existing duration tests for month and week cases. The work is done when the reported examples match the expected behavior without changing unrelated duration units.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
40/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.