Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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'] }
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading