Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting duration between two dates in date-fns in days:hrs:mins:secs format

I'm trying to display a count-down timer using date-fns library and doing things the below way, unable to find the solution in react.

Expected output: 60 days : 8 hours : 9 minutes : 5 seconds remaining

    const finishTime = new Date("01/01/2020");
    const currentTime = new Date();

Adding results to array to traverse later:

    results.push(differenceInMonths(finishTime, currentTime));
    results.push(differenceInDays(finishTime, currentTime));
    results.push(differenceInHours(finishTime, currentTime));
    results.push(differenceInMinutes(finishTime, currentTime));
    results.push(differenceInSeconds(finishTime, currentTime));

Adding manual logic to get the time from seconds. There should obviously be a better logic wiht / without using the library, which I'm missing:

    const monthsRemaining = results[4] / (30 * 24 * 3600); // this will anyways fail as 30 days is not common for every month
    const daysRemaining = (monthsRemaining % 1) * 30;
    const hoursRemaining = (daysRemaining % 1) * 24;
    const minutesRemaining = (hoursRemaining % 1) * 60;
    const secondsRemaining = (minutesRemaining % 1) * 60;

return (
        <div>
            {Math.round(monthsRemaining)} Months : {Math.round(daysRemaining)}{" "}
            days : {Math.round(hoursRemaining)} hours :{" "}
            {Math.round(minutesRemaining)} minutes :{" "}
            {Math.round(secondsRemaining)} seconds
        </div>
    );

Any suggestions or pointers to the right methods, as I don't see such direct implementation, I could only see formatDistance method which only one unit

like image 422
beta programmers Avatar asked Jan 26 '23 19:01

beta programmers


1 Answers

intervalToDuration is what you are looking for.

// Get the duration between January 15, 1929 and April 4, 1968.
intervalToDuration({
  start: new Date(1929, 0, 15, 12, 0, 0),
   end: new Date(1968, 3, 4, 19, 5, 0)
})
// => { years: 39, months: 2, days: 20, hours: 7, minutes: 5, seconds: 0 }

https://date-fns.org/v2.16.1/docs/intervalToDuration

like image 90
phenicie Avatar answered Jan 28 '23 09:01

phenicie