iamkun / iamkun/dayjs

Duration with ISO8601 months

Open
#2,243 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

> However, I'm not sure if we should count 2 months as 60 days or 61 days. You can tell from the unit test code I added.

_Originally posted by @iamkun in https://github.com/iamkun/dayjs/issues/949#issuecomment-652810292_

---

As already mentioned by @iamkun, there is a problem calculating durations for months relative to a date.

For example, as `P2M` (two months) can be either:
* 59 (28 days february + 31 days march 2023)
* 60 days (31 days in january and 29 days in february 2024)
* 61 (most cases)
* 62 (31 days in july and august 2021)

This leads to faulty results, as [demonstrated here](https://stackblitz.com/edit/dayjs-playground-iwkmc2?file=index.js).

---

So using the input `2020-04-07T09:00:00` and adding two months using the ISO 8601 duration `P2M`:

```ts
dayjs('2020-04-07T09:00:00').add(dayjs.duration('P2M')).format();
```
results in `2020-06-06T09:00:00+02:00`.

And

```ts
dayjs('2020-04-07T09:00:00').add(dayjs.duration({ months: 2 })).format();
```
results in `2020-06-06T09:00:00+02:00` as well.

\
More confusing to me is, that using the `add` method works without problems:
```ts
dayjs('2020-04-07T09:00:00').add(2, 'M').format();
```
results in `2020-06-07T09:00:00+02:00`.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the two examples using dayjs.duration('P2M'), dayjs.duration({ months: 2 }), and add(2, 'M'), then trace the duration handling and add integration. Done requires an agreed month-relative interpretation and consistent results for these cases, covered by regression tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
api
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.