iamkun / iamkun/dayjs

Adding weeks in duration plugin is broken

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

Description

**Describe the bug**

I haven't upgraded since `1.11.7` because of various bugs, and this is the big one now:

Sample code:

```javascript
const dayjs = require('dayjs')
dayjs.extend(require('dayjs/plugin/duration'))

const today = dayjs()
const twoWeeksFromNow = dayjs().add(dayjs.duration({weeks:2}))

console.log({today,twoWeeksFromNow})
```

(I have tried both `week` and `weeks`)

Here is what I get in `1.11.10`:

```
❯ node ./dayjs-bug.js
{
today: M {
'$L': 'en',
'$d': 2023-10-16T17:38:24.875Z,
'$y': 2023,
'$M': 9,
'$D': 16,
'$W': 1,
'$H': 13,
'$m': 38,
'$s': 24,
'$ms': 875,
'$x': {},
'$isDayjsObject': true
},
twoWeeksFromNow: M {
'$L': 'en',
'$d': 2023-10-16T17:38:24.875Z,
'$y': 2023,
'$M': 9,
'$D': 16,
'$W': 1,
'$H': 13,
'$m': 38,
'$s': 24,
'$ms': 875,
'$x': {},
'$isDayjsObject': true
}
}
```

**Expected behavior**

I expect this calculation to happen similarly to how it does in `1.11.7`:

```
❯ node ./dayjs-bug.js
{
today: M {
'$L': 'en',
'$d': 2023-10-16T17:41:20.524Z,
'$x': {},
'$y': 2023,
'$M': 9,
'$D': 16,
'$W': 1,
'$H': 13,
'$m': 41,
'$s': 20,
'$ms': 524
},
twoWeeksFromNow: M {
'$L': 'en',
'$d': 2023-10-30T17:41:20.524Z,
'$x': {},
'$y': 2023,
'$M': 9,
'$D': 30,
'$W': 1,
'$H': 13,
'$m': 41,
'$s': 20,
'$ms': 524
}
}
```

**Information**
- Day.js Version: 1.11.10
- OS: macOS 14.0
- Browser: Node in the CLI
- Time zone: `America/New_York`

Contributor guide

Open the contributing guide

Research direction

Start at the dayjs/plugin/duration entry point shown in the report and reproduce the weeks calculation with the provided Node example. Compare the behavior in versions 1.11.10 and 1.11.7; done means adding two weeks produces the expected date and the regression is covered by a test.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.