How to Compare Dates with Moment.js?
Last Updated :
25 Jul, 2024
Moment.js is a popular JavaScript library that developers use for parsing, manipulating, and formatting dates and times. It provides a range of methods to compare dates, making it easier to handle various date-related tasks. With Moment.js, you can use methods like isBefore, isAfter, diff, and isSame to compare dates
Run the below command before running the code in your local system:
npm i momentjs
These are the following approaches that we are going to discuss:
Using isBefore()
and isAfter()
Methods
We use the isBefore() and isAfter() methods from Moment.js to compare two dates. It first checks if date1 is before date2, then if date1 is after date2, and finally, if neither condition is true, it concludes that the dates are the same.
Syntax:
moment().isBefore(moment | string | number | Date | Array, granularity);
moment().isAfter(moment | string | number | Date | Array, granularity);
Example: The below example uses isBefore
and isAfter
Methods to Compare Dates with Moment.js.
JavaScript
// script.js
const moment = require('moment');
const date1 = moment('2024-07-20');
const date2 = moment('2024-07-22');
if (date1.isBefore(date2)) {
console.log(`Comparing dates:
date1: ${date1.format('YYYY-MM-DD')}
date2: ${date2.format('YYYY-MM-DD')}
Result: date1 is before date2`);
} else if (date1.isAfter(date2)) {
console.log(`Comparing dates:
date1: ${date1.format('YYYY-MM-DD')}
date2: ${date2.format('YYYY-MM-DD')}
Result: date1 is after date2`);
} else {
console.log(`Comparing dates:
date1: ${date1.format('YYYY-MM-DD')}
date2: ${date2.format('YYYY-MM-DD')}
Result: date1 is the same as date2`);
}
Output:
Comparing dates:
date1: 2024-07-20
date2: 2024-07-22
Result: date1 is before date2
Using diff()
Method
The diff() method from Moment.js to calculate the difference between two dates in milliseconds. If the difference is negative, it means date1 is before date2; if positive, date1 is after date2. If the difference is zero, the dates are the same.
Syntax:
moment().diff(moment | string | number | Date | Array, unit, precise);
Example: The below example uses d
iff Method to Compare Dates with Moment.js.
JavaScript
// script.js
const moment = require('moment');
const date1 = moment('2024-07-20');
const date2 = moment('2024-07-22');
const diff = date1.diff(date2);
if (diff < 0) {
console.log(`Comparing dates:
date1: ${date1.format('YYYY-MM-DD')}
date2: ${date2.format('YYYY-MM-DD')}
Result: date1 is before date2`);
} else if (diff > 0) {
console.log(`Comparing dates:
date1: ${date1.format('YYYY-MM-DD')}
date2: ${date2.format('YYYY-MM-DD')}
Result: date1 is after date2`);
} else {
console.log(`Comparing dates:
date1: ${date1.format('YYYY-MM-DD')}
date2: ${date2.format('YYYY-MM-DD')}
Result: date1 is the same as date2`);
}
Output:
Comparing dates:
date1: 2024-07-20
date2: 2024-07-22
Result: date1 is before date2
Using isSame()
Method
In this approach, we are using the isSame() method from Moment.js to determine if two dates are the same on a specific granularity (in this case, 'day'). If they are not the same, the script further checks if date1 is before or after date2.
Syntax:
moment().isSame(moment | string | number | Date | Array, granularity);
Example: The below example uses isSame
Method to Compare Dates with Moment.js.
JavaScript
// script.js
const moment = require('moment');
const date1 = moment('2024-07-20');
const date2 = moment('2024-07-22');
if (date1.isSame(date2, 'day')) {
console.log(`Comparing dates:
date1: ${date1.format('YYYY-MM-DD')}
date2: ${date2.format('YYYY-MM-DD')}
Result: date1 is the same as date2`);
} else if (date1.isBefore(date2, 'day')) {
console.log(`Comparing dates:
date1: ${date1.format('YYYY-MM-DD')}
date2: ${date2.format('YYYY-MM-DD')}
Result: date1 is before date2`);
} else if (date1.isAfter(date2, 'day')) {
console.log(`Comparing dates:
date1: ${date1.format('YYYY-MM-DD')}
date2: ${date2.format('YYYY-MM-DD')}
Result: date1 is after date2`);
}
Output:
Comparing dates:
date1: 2024-07-20
date2: 2024-07-22
Result: date1 is before date2
Similar Reads
How To Compare Only Date In Moment.js?
In Moment.js, date manipulation and parsing can be done efficiently. Along with this, the library provides ways to compare only the date portion of two date objects, ignoring the time component. Methods like startOf, isSame, and formatting techniques enable you to focus solely on the date part for a
3 min read
How to Format Date with Moment.js?
Formatting dates is essential for presenting date and time information in a way that's readable and useful for users. Moment.js provides several methods for formatting dates from simple and predefined formats to more complex and localized representations. Below are different approaches: Table of Con
2 min read
How To Set Time With Date in Moment.js?
Moment.js is a powerful JavaScript library that makes it easy to work with dates and times. Sometimes, we want to set a specific time to an already existing date. For example- we have a date like "2024-08-15" and we want to set the time to "9:00 AM". Moment.js provides simple ways to do this. In thi
2 min read
How to Remove Time From Date with Moment.js?
Moment.js is a widely used JavaScript library for managing and manipulating dates and times. It simplifies complex date operations and makes it easier to work with dates in a consistent format. One common task is to extract just the date portion from a full timestamp, ignoring the time part. This is
2 min read
How to Compare Two Date Strings in TypeScript ?
In TypeScript, we can compare two date strings by converting them to comparable formats using Date objects or by parsing them into the timestamps. We will discuss different approaches with the practical implementation of each one of them. Table of Content Using Date ObjectsUsing Date.parseUsing Intl
4 min read
How to Use Moment.js with Vuejs?
The Moment.js library can be integrated with Vue.js to simplify date and time manipulation within your Vue components. By importing Moment.js and using its formatting functions, you can easily display and format dates in your application. This integration allows you to use Moment.js's powerful featu
2 min read
How to Check Whether an Object is a Date ?
This article will show you how to check whether the given object is a Date or not. There are two methods to check for date objects, which are described below: Method 1: Using instanceof Operator The instanceof operator checks whether the prototype property of a constructor appears anywhere in the pr
2 min read
How to Subtract 7 Days from Current Date with MomentJS?
The article is all about how can we get the 7 days subtracted date from the given date. for eg: if the given date is 28-08-2024 then it should print 21-08-2024, that is the exactly subtraction of the seven day from the given day. These are the following approaches: Table of Content Using the subtrac
2 min read
How to use moment.js to change date format in jQuery ?
Using Moment.js to change date format in jQuery involves importing the Moment.js library and utilizing its functions to parse, manipulate, and format dates. You can easily convert dates into various formats by calling moment(date).format(desiredFormat) to display the date in your preferred format. m
2 min read
How to get Yesterday's Date with Momentjs?
MomentJS works on Date and Time Manipulation to provide a convenient way to handle, manipulate, and format dates and times in JavaScript. It simplifies common date operations such as parsing, validating, manipulating, and displaying dates. To get yesterday's date using Moment.js, you can use various
2 min read