Web development and Tech news Blog site. WEBISFREE.com

HOME > js

[momentjs] Find out how much difference there is between today's date and a specific date in days, hours, and minutes

Last Modified : 03 Oct, 2023 / Created : 03 Oct, 2023
View Count
This is a guide on using the date and time library momentjs to find out the difference between the current time and another specific time in days, hours, and minutes.

# Finding the Difference in Date and Time with momentjs

Firstly, suppose you have the following date and time. How would you find out how much time has passed from today? For instance, this is often used to display how much time has passed since a new post was written instead of showing the timestamp, as shown in the screenshot below.

As you can see in the screenshot, it is marked as "14 minutes ago," showing that 14 minutes have passed. It differs from the section below showing today’s date. Showing the elapsed time this way can inform how recently a post was made, with the advantage of immediately identifying recent posts.

To find the difference between two dates, you can use the diff() method in momentjs.

! Understanding the momentjs diff() Method

The diff() method used on two dates is a straightforward method that tells how much difference there is between two values in the unit you want (days, hours, minutes). Below is the simple syntax.

moment(ADate).diff(moment(BDate), optionValue)

Here's how each value is used:

// ADate, BDate : The two date values to compare
// optionValue : Unit value to be displayed as 'days' | 'hours' | 'minutes'

Want to find out how many days apart they are? In this case, you can use the value 'days'. Let's look at some examples below.

@ Find out how many days are between today and a specific date
Below is how to find out how many days are between today's date and a specific date, using 'days' to get the value.
const diffDays = moment('2023-10-01').diff(moment('2023-10-03'), 'days');

// Result

The above example, which switched today's date and a specific date to show a negative value, will display 2 if the two values are switched back and run again.
const diffDays = moment('2023-10-03').diff(moment('2023-10-01'), 'days');

// Result

As expected, we were able to get a positive value of 2. The above example was to obtain the date difference value, next, let’s find out how many hours and minutes apart they are.

@ Find out how many hours and minutes are between today and a specific date
const diffHours = moment('2023-10-03 12:00:00').diff(moment('2023-10-03 02:00:00'), 'hours');
const diffMinutes = moment('2023-10-03 12:00:00').diff(moment('2023-10-03 02:00:00'), 'minutes');
const diffMinutes

// Result

As you can see from the result of this example, it returns 10 hours and then 600 minutes, accurately showing the difference.

This concludes our guide on using momentjs’s diff() to obtain the difference in values.
Perhaps you're looking for the following text as well?


    Setting Specific Range of Node or NPM version in package.json


    [JavaScript] How to draw gradient colors on canvas