iamkun / iamkun/dayjs

Month threshold is not used when rounding years

Open
#2,232 1 comment 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**
Having set month and year thresholds for the relative time plugin, the monthly threshold is not used for rounding values greater than 1 year.

``` Javascript
import dayjs from "dayjs";
import relativeTime from "dayjs/plugin/relativeTime";
import updateLocale from "dayjs/plugin/updateLocale";

const strictThresholds = [
{ l: "M", r: 1 },
{ l: "MM", r: 11, d: "month" },
{ l: "y", r: 1 },
{ l: "yy", d: "year" }
];

dayjs.extend(relativeTime, { thresholds: strictThresholds });
dayjs.extend(updateLocale);
dayjs.updateLocale("en", {
relativeTime: {
M: "1 month",
MM: "%d months",
y: "1 year",
yy: "%d years"
}
});

const TODAY = "2023-02-07";
const tenMonthsAgo = dayjs(TODAY).from(dayjs("2022-04-16"), true); // 10 months
const twentyTwoMonthsAgo = dayjs(TODAY).from(dayjs("2021-04-16"), true); // 2 years -- should be 1 year
const thirtyFourMonthsAgo = dayjs(TODAY).from(dayjs("2020-04-16"), true); // 3 years -- should be 2 years

```

[Demo CodeSandbox](https://codesandbox.io/s/loving-wildflower-ic7vdy?file=/src/App.js)

**Expected behavior**
The rounding of values for years should use the monthly threshold.

**Information**
- Day.js Version: 1.11.7
- OS: IOS
- Time zone: GMT-07:00 DST (Pacific Daylight Time)

Contributor guide

Open the contributing guide

Research direction

Start by running the provided relativeTime plugin reproduction with the strictThresholds configuration and inspect the relativeTime plugin entry point. Verify how month and year values are rounded for 22 and 34 months; done means the examples return 1 year and 2 years while the 10-month case remains 10 months.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.