-1

When using date-fns, how can I use intlFormatDistance such that for a duration less than 24 hours, it displays "today" or "yesterday", instead of "x hours ago".

Example:

intlFormatDistance(myDate, new Date());
// Default output: 13 hours ago
// Desired output: Today
0

1 Answer 1

0

Wrap it

const customIntlFormatDistance = (date, baseDate = new Date()) => 
  differenceInHours(baseDate, date) < 24 ? "Today" :
    isYesterday(date) ? "Yesterday" : 
      intlFormatDistance(date, baseDate);

In case you meant "if today" instead of < 24 then:

const customIntlFormatDistance = (date, baseDate = new Date()) => 
  isToday(date) ? "Today" : 
    isYesterday(date) ? "Yesterday" : 
      intlFormatDistance(date, baseDate);
2
  • A bit unintuitive, but ok, @jabaa
    – mplungjan
    Commented Feb 5 at 13:41
  • 2
    It's not my question.
    – jabaa
    Commented Feb 5 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.