-
Notifications
You must be signed in to change notification settings - Fork 521
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
base: master
Are you sure you want to change the base?
format relative time #321
Conversation
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. | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
*/ | |
locale - String representing the locale to be used. Defaults to "en" | |
*/ |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rtf(2, 'minutes'); // in 2 min | |
rtf(2, 'minutes'); // in 2 min | |
rtf(2, 'minutes', 'es'); // dentro de 2 minutos |
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.