Like any kind of apps, there are difficult issues to solve when we write JavaScript apps.

In this article, we’ll look at some solutions to common JavaScript problems.

Get the Time Difference Between two Date-Times and Format it in HH:mm:ss Format

To get the time difference between 2 date times, we can use the diffmethod from moment.js

This way, we don’t have to do the calculations ourselves,

For instance, we can write:

const earlierDateTime = "04/09/2020 14:00:00";
const laterDateTime = "04/09/2020 16:20:30";

const difference = moment(laterDateTime, "DD/MM/YYYY HH:mm:ss").diff(moment(earlierDateTime, "DD/MM/YYYY HH:mm:ss"))
const diff = moment.utc(difference).format("HH:mm:ss");

We call diff to get the difference of laterDateTime and earlierDateTimeand convert the difference to UTC.

This will output the difference in HH:mm:ss format.

We get ‘02:20:30' as the value of diff .

If the difference between the 2 date-times is longer than today, then we’ve to some extra calculations.

#programming #technology #software-development #javascript #web-development

JavaScript Tips — Calculating Time Difference, The Remainder Operator
1.35 GEEK