How To Compare Only Date In Moment.js?
Last Updated :
01 Aug, 2024
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 accurate comparisons.
Run the below command before running the code in your local system:
npm i moment
In this approach, we are using the format method to convert both date1 and date2 to strings in the 'YYYY-MM-DD' format, which represents only the date part of each moment object. By comparing these formatted date strings, we determine if the two dates are the same, ignoring the time component.
Example: The below example uses Date Comparison and format to compare only dates in moment.js.
JavaScript
// script.js
const moment = require("moment");
const date1 = moment("2024-07-24T08:00:00");
const date2 = moment("2024-07-24T14:30:00");
const res = date1.format("YYYY-MM-DD") === date2.format("YYYY-MM-DD");
console.log(`Are dates the same? ${res}`);
Output:
Are dates the same? true
2. Using isSame method
In this approach, the isSame method is used with the 'day' unit to compare date1 and date2. This method focuses on the date part of each moment object, ignoring any differences in time. By comparing using 'day', it determines if both dates fall on the same calendar day, which makes sure that only the date is considered in the comparison.
Example: The below example uses isSame to compare only date in moment.js.
JavaScript
// script.js
const moment = require("moment");
const date1 = moment("2024-07-24T08:00:00");
const date2 = moment("2024-07-24T14:30:00");
const res = date1.isSame(date2, "day");
console.log(`Are dates the same? ${res}`);
Output:
Are dates the same? true
3. Using startOf and diff
In this approach, we use the startOf method to set both date1 and date2 to the start of their respective days, removing any time component. We then calculate the difference in days between these adjusted dates using the diff method. By checking if this difference is zero, we determine if both dates are on the same calendar day.
Example: The below example uses startOf and diff to compare only date in moment.js.
JavaScript
// script.js
const moment = require("moment");
const date1 = moment("2024-07-24T08:00:00");
const date2 = moment("2024-07-25T14:30:00");
const startOfDay1 = date1.startOf("day");
const startOfDay2 = date2.startOf("day");
const dayDiff = startOfDay1.diff(startOfDay2, "days");
const res = dayDiff === 0;
console.log(`Are dates the same? ${res}`);
Output:
Are dates the same? false
Similar Reads
How to Compare Dates with Moment.js? 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 isSam
3 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.mo
2 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 Conte
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 ContentUsing Date ObjectsUsing Date.parseUsing Intl.
4 min read
Moment.js Customize Invalid Date The Locale#invalidDate in Moment.js: The invalidate property should be a string, a replacement for the "Invalid Date" message you want to customize. Before proceeding, please install the moment.js library using the following command. Installation: npm install moment Syntax: const moment = require('m
2 min read