Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom unit option of relativeTime #565

Closed
ktmouk opened this issue Oct 14, 2023 · 2 comments · Fixed by #566
Closed

Support custom unit option of relativeTime #565

ktmouk opened this issue Oct 14, 2023 · 2 comments · Fixed by #566
Labels
enhancement New feature or request unconfirmed Needs triage.

Comments

@ktmouk
Copy link
Contributor

ktmouk commented Oct 14, 2023

Is your feature request related to a problem? Please describe.

format.relativeTime currently only supports returning a relative time using an automatically calculated unit in this function.
However, there may be cases that we sometimes want to use a specific unit to develop an application.

For example, if we were to create a timeline UI that groups photos by date in a photo album app, we might want to specify the day unit when creating a title for the timeline, since, with other units, it might get too detailed.

Describe the solution you'd like

One solution I came up with is to add a unit argument to relativeTime function, like this:

const dateTime = new Date('2020-11-20T00:00:00Z');
const now = new Date('2020-11-20T01:00:00Z');

const format = useFormatter();
format.relativeTime(dateTime, now, 'day');  // returns 'today', not '1 hour'

Describe alternatives you've considered

We can also use RelativeTimeFormat directly instead:

console.log(new Intl.RelativeTimeFormat('en', { numeric: 'auto' }).format(0, 'day')); // 'today'
@ktmouk ktmouk added enhancement New feature or request unconfirmed Needs triage. labels Oct 14, 2023
@amannn
Copy link
Owner

amannn commented Oct 14, 2023

Seems like a good enhancement, yes!

API-wise I have two thoughts:

  1. Maybe we should accept an object, in case we allow to configure further properties in the future. E.g. {unit: 'day'}.
  2. now is an optional parameter, maybe we should overload the second argument to avoid something like format.relativeTime(dateTime, undefined, {unit: 'day'}).

Based on this, format.relativeTime(dateTime: Date, nowOrOptions: Date | {now?: Date, unit?: string}) could be an idea.

What do you think? Would you be interested in contributing this feature and the corresponding docs?

@ktmouk
Copy link
Contributor Author

ktmouk commented Oct 14, 2023

Thank you for your reply! Your idea is superior to mine. I would like to contribute to this.

amannn added a commit that referenced this issue Oct 18, 2023
This PR adds the `relativeTime` options to support custom units and
updates the documentation accordingly.
Close: #565

---------

Co-authored-by: Jan Amann <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request unconfirmed Needs triage.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants