skip to Main Content

I am using below logic to calculate the time difference.

However, this is giving wrong result value when days > 1.
Please help simplifying the code.

I will be using the time difference logic in angular framework.

const startDate = new Date(form.value.startDate);
const endDate = new Date();
const dateDiff = this.calculateDateDifference(startDate, endDate);

calculateDateDifference(startDate, endDate) {
    const difference = endDate.getTime() - startDate.getTime();

    const seconds = Math.floor((difference / 1000) % 60);
    const minutes = Math.floor((difference / (1000 * 60)) % 60);
    const hours = Math.floor((difference / (1000 * 60 * 60)) % 24);
    const days = Math.floor(difference / (1000 * 60 * 60 * 24));

    console.log(days + ' days ' + hours + ' hours ' + minutes + ' minutes ' + seconds + ' seconds');
    if (days > 1) {
        return `${days} Days, ${hours} Hours, ${minutes} Mins`;
    } else {
        return `${hours} Hours, ${minutes} Mins`;
    }
}

How can I achieve the same output using "moment"?

2

Answers


  1. As mentioned by jabaa, you should avoid moment.js as it is now in a maintenance state.

    You can try out day.js which is a modern equivalent and won’t require a lot of effort to modify your code.

    You can install it via yarn/npm with the package name dayjs

    You can then write something like:

    const startDate = new Date("2024-03-18")
    const endDate = new Date("2024-03-18 17:00");
    console.log(calculateDateDifference(startDate, endDate))
    
    const startDate2 = new Date("2024-03-10")
    const endDate2 = new Date();
    console.log(calculateDateDifference(startDate2, endDate2))
    
    function calculateDateDifference(startDate, endDate) {
        const difference = dayjs(endDate).diff(startDate, 'millisecond');
    
        const seconds = Math.floor((difference / 1000) % 60);
        const minutes = Math.floor((difference / (1000 * 60)) % 60);
        const hours = Math.floor((difference / (1000 * 60 * 60)) % 24);
        const days = Math.floor(difference / (1000 * 60 * 60 * 24));
    
        if (days > 1) {
            return `${days} Days, ${hours} Hours, ${minutes} Mins`;
        } else {
            return `${hours} Hours, ${minutes} Mins`;
        }
    }
    <script src="https://cdn.jsdelivr.net/npm/dayjs@1/dayjs.min.js"></script>

    I appreciate you mentioned angular, to allow the running on Stackoverflow I just used plain JS and HTML, but this should work on Angular also.

    Login or Signup to reply.
  2. const moment = require('moment');
    
    function calculateDateDifference(startDate, endDate) {
        const duration = moment.duration(endDate.diff(startDate));
    
        const days = duration.days();
        const hours = duration.hours();
        const minutes = duration.minutes();
        const seconds = duration.seconds();
    
        console.log(`${days} days ${hours} hours ${minutes} minutes ${seconds} seconds`);
        
        if (days > 1) {
            return `${days} Days, ${hours} Hours, ${minutes} Mins`;
        } else {
            return `${hours} Hours, ${minutes} Mins`;
        }
    }
    
    const startDate = moment(form.value.startDate);
    const endDate = moment();
    const dateDiff = calculateDateDifference(startDate, endDate);
    

    maybe it is useful for you I converted that code in the moment

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search