How to Get time Difference Between Datetimes in MomentJS?
Last Updated :
26 Jul, 2024
MomentJS is a popular JavaScript library used for parsing, validating, manipulating, and formatting dates. One common requirement when working with dates is calculating the difference between two date times.
Run the below command before running the code in your local system:
npm i moment
These are the following approaches:
Using the diff() method
In this approach, we are using the diff() method. diff method is the most straightforward way to get the difference between two datetimes in MomentJS. It calculates the difference in milliseconds by default but can be customized to return the difference in other units.
Syntax:
moment(datetime1).diff(moment(datetime2), unit, precise);
Where-
- datetime1: The first datetime.
- datetime2: The second datetime to compare against the first.
- unit: The unit of measurement (optional, e.g., 'days', 'hours', 'minutes', 'seconds').
- precise: Boolean to return a precise difference (optional).
Example: In the below example we have used the diff method to get the difference between datetimes.
JavaScript
// Difference in milliseconds
const moment = require('moment');
let datetime1 = moment('2024-07-25T12:00:00');
let datetime2 = moment('2024-07-25T10:00:00');
let diffInMillis = datetime1.diff(datetime2);
console.log(`Difference in milliseconds: ${diffInMillis}`);
// Difference in hours
let diffInHours = datetime1.diff(datetime2, 'hours');
console.log(`Difference in hours: ${diffInHours}`);
Output:
Difference in milliseconds: 7200000
Difference in hours: 2
Using from() and fromNow() method
In this approach we will use from() and fromNow() method. MomentJS provides this methods to display the time difference between two datetimes in a human-readable format. These methods make it easy to present date differences in a more intuitive way, such as "2 hours ago" or "in 3 days."
Syntax:
moment(datetime1).from(moment(datetime2));
moment(datetime1).fromNow();
Example: In below example we will use from and fromNow methods to get difference between datetimes.
JavaScript
const moment = require('moment');
let datetime1 = moment('2024-07-25T12:00:00');
let datetime2 = moment('2024-07-25T10:00:00');
let humanReadableDiff = datetime1.from(datetime2);
console.log(humanReadableDiff);
let datetimeNow = moment();
let datetimeFuture = moment().add(2, 'hours');
let humanReadableDiffNow = datetimeFuture.fromNow();
console.log(humanReadableDiffNow);
Output:
in 2 hours
in 2 hours
Similar Reads
How to Get Hours Difference Between Two Dates in Moment Js? Moment.js library has a set of powerful functions for date and time manipulation, including calculating differences between dates. To get the hours difference between two dates, you can use various methods provided by Moment.js. Run the below command before running the code in your local system:npm
2 min read
How to Find Numeric Difference Between Two Dates in MomentJS? To find the numeric difference between two dates using MomentJS, you would typically need to first parse the dates into MomentJS objects. Then, you calculate the difference between these two dates. This difference is usually represented as a numeric value, indicating the quantity of a specific unit
3 min read
How To Extract time From MomentJS Object? MomentJS is a powerful library for handling dates and times in JavaScript. It's widely used for formatting, manipulating, and parsing dates. Often, while working with MomentJS, you might need to extract just the time part from a MomentJS object. These are the approaches for extracting time from the
2 min read
How to Format Datetime to YYYY-MM-DD HH:MM:SS in Moment.js? In Moment.js, formatting the date in the proper format is important for consistent data presentation and manipulation. The library provides various methods to handle and transform dates. You can use direct formatting with format(), manipulate ISO strings, or work with Unix timestamps to achieve the
2 min read
How to Get the Beginning and End of the Day with MomentJS? To get the beginning and end of the day using Moment.js, you can manipulate a date to represent the start or end of that day. The beginning of the day is typically set to midnight (00:00:00), while the end of the day is set to just before midnight (23:59:59). This is useful for operations that requi
2 min read