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 without time #1218

Open
Vincz opened this issue Jun 24, 2019 · 16 comments
Open

Format relative without time #1218

Vincz opened this issue Jun 24, 2019 · 16 comments

Comments

@Vincz
Copy link

Vincz commented Jun 24, 2019

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

@kossnocorp
Copy link
Member

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

@Vincz
Copy link
Author

Vincz commented Jun 25, 2019

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).

@Jav3k
Copy link

Jav3k commented Mar 15, 2020

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

@boaz-codota
Copy link

This is a highly needed feature. Is it possible to add this? I think I'd be happy to work on it.

@jesusantguerrero
Copy link

Anybody working on this one?

@shakedlokits
Copy link

Still needed

@KimCalvin
Copy link

A year later and still needed.

@mikerudge
Copy link

+1

1 similar comment
@wouter173
Copy link

+1

@uwuru
Copy link

uwuru commented Aug 25, 2023

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?

  1. 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.
  2. 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 😄

@clyncha
Copy link

clyncha commented Dec 11, 2023

@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?

@teniolafatunmbi
Copy link

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]

@jckw
Copy link

jckw commented Mar 23, 2024

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.

@nestornavas
Copy link

+1. Still needed

@Kifoxive
Copy link

+1 =)

@jckw
Copy link

jckw commented Apr 21, 2024

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.

Current bounty reward

To make this a public bounty or have a reward split, the maintainer can reply to this comment.

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

No branches or pull requests