Using moment to calculate time difference

61 views Asked by At

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"?


3

There are 3 answers

1
Tom Roman On

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.

1
codewithharshad On
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

3
Rafael Zulianeli On

I hope it helps.

you can use "Subtract".

var dateA = moment("2024-03-19")
var dateB = moment("2024-03-20")
var millisecondsDiff = dateA.subtract(dateB.valueOf(), 'milliseconds')

this way you can get the value of the difference between two dates with Moment.js