Search code examples
javascriptdate-fns

How to remove time count from intervalToDuration in date-fns


How to remove time countdown from intervalToDuration in date-fns?

 let duration = intervalToDuration({
    start: new Date('2022-03-24'),
    end: new Date(),
  });

  const newD = formatDuration(duration, {
    delimiter: ', ',
  });
  console.log(newD);

From the above function result is 6 months, 5 days, 4 hours, 8 minutes, x seconds

What I'm expecting is 6 months, 5 days


Solution

  • You'll have to adjust the dates used to calculate the duration.

    In case you want to change the formatted output, not the duration itself:

    formatDuration accepts a format in its options: https://date-fns.org/v2.29.3/docs/formatDuration

    E.g.

    formatDuration(
      {
        months: 9,
        days: 7,
        hours: 5,
        minutes: 9,
        seconds: 30
      },
      { format: ['months', 'days'] }
    )