date-fns
date-fns copied to clipboard
Format relative without time
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
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 formatRelative.
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 withoutTime option.
I was switching from luxon, and it's a feature I was missing.
Sure, it's a lot of work, but there is already everything necessary. We would just need to split the relative formats (at least for the locales I know).
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
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?
- Just add this functionality to locales we are familiar with, making it optional for a locale to support it. Then people familiar with their locale can come and add the support later as they desire the functionality.
- We do best efforts using services like Google Translate to update all locales. This sounds risky, and it is, but this functionality would be opt in not affecting the existing behaviour and people can retrospectively improve upon our efforts.
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.
A way around this is to split the return string from formatRelative with the 'at' substring and get the string in the first index (index 0). The time always come after the 'at' substring so it's a safe bet, for now.
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.