iamkun / iamkun/dayjs

Cannot set thresholds on a new locale

Open
#1,660 1 comment 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**
Trying to create a new locale and pass in thresholds such that '23 hours ago' would be '23h' and anything less than 24hours would not show up as days.

Code
`import dayjs from '@/js/be/dayJSPlugins.js';

const localeObject = {
name: 'en-segment',
relativeTime: {
future: 'in %s',
past: '%s',
s: '%ds',
m: '%dm',
mm: '%dm',
h: '%dh',
hh: '%dh',
d: '%dd',
dd: '%dd',
M: '%dM',
MM: '%dM',
y: '%dy',
yy: '%dy',
},
};

export function getRelativeTime(timestamp) {
dayjs.locale('en-segment', localeObject);
delete relativeTime.$i // this allow plugin to be installed again
dayjs.extend(relativeTime, {
rounding: Math.floor,
thresholds: [
{ l: 's', r: 60 },
{ l: 'm', r: 60 },
{ l: 'h', r: 24 },
],
});

return dayjs.unix(timestamp).fromNow();
}
`

**Information**
- Day.js Version 1.10.6
- OS: MacOS Catalina
- Browser Chrome 93
- Time zone: GMT-07:00 DST (Pacific Daylight Time)

Contributor guide

Open the contributing guide

Research direction

Start by reading js/be/dayJSPlugins.js and the relativeTime plugin setup shown in the report, then reproduce the custom locale with the supplied thresholds. Check how the plugin handles thresholds after installation and verify that times under 24 hours remain in hours while 23 hours is formatted as 23h.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.