iamkun / iamkun/dayjs

Duration .weeks() behaves differently

Open
#2,854 0 comments 0 reactions 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**
`.weeks()` behaves differently than `.days()` when working with duration. We have the following code example
```js
const duration = dayjs.duration(value);

return {
years: duration.years(),
months: duration.months(),
weeks: duration.weeks(),
days: duration.days(),
hours: duration.hours(),
minutes: duration.minutes(),
seconds: duration.seconds(),
milliseconds: duration.milliseconds(),
};
```

Where `value` is an ISO8601 duration format, for example: "P1M". When using "P1M" we get back `4` for from `.weeks()`, but `.days()` returns `0`.

So "P1M" returns:
```
{
years: 0,
months: 1,
weeks: 4,
days: 0,
hours: 0,
minutes: 0,
seconds: 0,
milliseconds: 0
}
```

And "P1Y" returns:
```
{
years: 1,
months: 0,
weeks: 52,
days: 0,
hours: 0,
minutes: 0,
seconds: 0,
milliseconds: 0
}
```

**Expected behavior**
I would expect weeks to behave the same as the other values, so when passing "P1M" I would expect a value of 0 for weeks. I saw in the code that there is an exception for the week unit here: https://github.com/iamkun/dayjs/blob/dev/src/plugin/duration/index.js#L197, removing that if statement exception fixes my issue, but not sure if that would cause other issues.

**Information**
- Day.js Version: 1.11.0
- OS: Mac OS Version 15.2 (24C101)
- Browser: Firefox 136.0.3 (aarch64)
- Time zone: GMT+2

Contributor guide

Open the contributing guide

Research direction

Start in src/plugin/duration/index.js around line 197, where the issue identifies the special handling for the week unit. Reproduce the reported results for ISO 8601 durations P1M and P1Y, then verify that weeks() matches the expected component behavior without regressing other duration units.

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
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.