-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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 without time #1218
Comments
Maybe we could add an option for that, but that will require updating all of the locales, and that's a lot of work. I'm not sure if it's worth it because you can create a locale with custom See: https://date-fns.org/v2.0.0-alpha.37/docs/I18n-Contribution-Guide#creating-a-locale-with-the-same-language-as-another-locale and: https://github.com/date-fns/date-fns/blob/master/src/locale/en-US/_lib/formatRelative/index.js |
Yes, I ended up overriding the formatRelative for the locales I use to add a |
Example of a solution offered by @kossnocorp import enGB from 'date-fns/locale/en-GB';
import formatRelative from 'date-fns/formatRelative';
import addDays from 'date-fns/addDays'
// https://date-fns.org/docs/I18n-Contribution-Guide#formatrelative
// https://github.com/date-fns/date-fns/blob/master/src/locale/en-US/_lib/formatRelative/index.js
// https://github.com/date-fns/date-fns/issues/1218
// https://stackoverflow.com/questions/47244216/how-to-customize-date-fnss-formatrelative
const formatRelativeLocale = {
lastWeek: "'Last' eeee",
yesterday: "'Yesterday'",
today: "'Today'",
tomorrow: "'Tomorrow'",
nextWeek: "'Next' eeee",
other: 'dd.MM.yyyy',
};
const locale = {
...enGB,
formatRelative: (token) => formatRelativeLocale[token],
};
const tomorrow = addDays(new Date, 1)
console.log(formatRelative(tomorrow, new Date(), { locale })) // Tomorrow |
This is a highly needed feature. Is it possible to add this? I think I'd be happy to work on it. |
Anybody working on this one? |
Still needed |
A year later and still needed. |
+1 |
1 similar comment
+1 |
I have been looking at this as I would find it useful myself. What would be the preferred approach be in regards to updating the many locales?
Which best aligns with the objectives and ways of working for this project? I highly expect the answer is option 1 but best to check and not assume 😄 |
@kossnocorp I want to format the string but I don't know what locale the user will have set for their browser. If they are viewing the website in Spanish how would that work? |
Still needed. Example: formatRelative(date, new Date()).split('at')[0] |
I just contributed to the bounty on this issue: https://until.dev/bounty/date-fns/date-fns/1218 The current bounty for completing it is $10.00 if it is closed within 1 month, and decreases after that. |
+1. Still needed |
+1 =) |
I just contributed to the bounty on this issue. Each contribution to this bounty has an expiry time and will be auto-refunded to the contributor if the issue is not solved before then. To make this a public bounty or have a reward split, the maintainer can reply to this comment. |
Is it possible to relative format a date but without the time ?
I'd like to have just "tomorrow" or "today" and not the "tomorrow at".
Any idea?
Thanks
The text was updated successfully, but these errors were encountered: