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

format relative time #321

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

format relative time #321

wants to merge 1 commit into from

Conversation

walebant
Copy link

With the Intl.RelativeTimeFormat constructor, we can format relative time in a locale-sensitive manner with ease. This lets us format relative time strings without the hassle that comes from manipulating strings.

@elkarouani
Copy link
Contributor

Good work @walebant, better than using the famous package "moment" !

unit - Unit to use in the relative time internationalized message.
Possible values are: "year", "quarter", "month", "week", "day", "hour", "minute", "second".
Plural forms are also permitted.
*/
Copy link

@WiXSL WiXSL Dec 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*/
locale - String representing the locale to be used. Defaults to "en"
*/

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great if the local could be passed as an optional parameter.

@@ -0,0 +1,15 @@
```javascript
const rtf = (value, unit) => new Intl.RelativeTimeFormat('en').format(value, unit);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const rtf = (value, unit) => new Intl.RelativeTimeFormat('en').format(value, unit);
const rtf = (value, unit, locale = 'en') => new Intl.RelativeTimeFormat(locale).format(value, unit);

// Example
rtf(5, 'day'); // in 5 days
rtf(-3, 'day'); // 3 days ago
rtf(2, 'minutes'); // in 2 min
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
rtf(2, 'minutes'); // in 2 min
rtf(2, 'minutes'); // in 2 min
rtf(2, 'minutes', 'es'); // dentro de 2 minutos

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants