date-fns icon indicating copy to clipboard operation
date-fns copied to clipboard

Format relative without time

Open Vincz opened this issue 6 years ago • 16 comments

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

Vincz avatar Jun 24 '19 18:06 Vincz

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

kossnocorp avatar Jun 25 '19 08:06 kossnocorp

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

Vincz avatar Jun 25 '19 09:06 Vincz

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

Jav3k avatar Mar 15 '20 08:03 Jav3k

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

boaz-codota avatar Feb 07 '21 14:02 boaz-codota

Anybody working on this one?

jesusantguerrero avatar Mar 03 '22 12:03 jesusantguerrero

Still needed

shakedlokits avatar Apr 16 '22 06:04 shakedlokits

A year later and still needed.

KimCalvin avatar May 31 '23 15:05 KimCalvin

+1

mikerudge avatar Aug 03 '23 11:08 mikerudge

+1

wouter173 avatar Aug 22 '23 20:08 wouter173

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 😄

uwuru avatar Aug 25 '23 17:08 uwuru

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

clyncha avatar Dec 11 '23 21:12 clyncha

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]

teniolafatunmbi avatar Mar 05 '24 22:03 teniolafatunmbi

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.

jckw avatar Mar 23 '24 21:03 jckw

+1. Still needed

nestornavas avatar Apr 02 '24 01:04 nestornavas

+1 =)

Kifoxive avatar Apr 18 '24 21:04 Kifoxive

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.

jckw avatar Apr 21 '24 13:04 jckw