The Wayback Machine - https://web.archive.org/web/20240814152907/https://www.geeksforgeeks.org/compare-two-dates-using-javascript/
Open In App

Compare two dates using JavaScript

Last Updated : 27 May, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Comparing two dates in JavaScript involves converting them to Date objects and using comparison operators or methods like getTime(). We compare dates to determine chronological order, check event timings, validate date ranges, or manage scheduling in applications.

In JavaScript, use getTime() to convert dates to numeric values for direct comparison.

Example 1: This example illustrates the comparison of the dates using the getTime() function.

javascript
// Current Date
let g1 = new Date();
let g2 = new Date();
if (g1.getTime() === g2.getTime())
    console.log("Both  are equal");
else
    console.log("Not equal");

Output
Both  are equal

Example 2: This example illustrates the comparison of the current date with the assigned date using the getTime() function.

JavaScript
let g1 = new Date();

// (YYYY-MM-DD)
let g2 = new Date("2019-08-03");
if (g1.getTime() < g2.getTime())
    console.log("g1 is lesser than g2");
else if (g1.getTime() > g2.getTime())
    console.log("g1 is greater than g2");
else
    console.log("both are equal");

Output
g1 is greater than g2

Example 3: This example illustrates the comparison of 2 given dates using the getTime() function.

JavaScript
let g1 = new Date("2019, 08, 03, 11, 45, 55");

// (YYYY, MM, DD, Hr, Min, Sec)
let g2 = new Date("2019, 08, 03, 10, 22, 42");
if (g1.getTime() < g2.getTime())
    console.log("g1 is lesser than g2");
else if (g1.getTime() > g2.getTime())
    console.log("g1 is greater than g2");
else
    console.log("both are equal");

Output
both are equal

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.



Similar Reads

How to store all dates in an array present in between given two dates in JavaScript ?
Given two dates the task is to get the array of dates between the two given dates using JavaScript. Below are the following approaches: Table of Content Using push() MethodUsing for loop and push() MethodUsing concat methodApproach 1: Using push() MethodSelect the first and last date and store it in a variable.Check if the start date is less than t
3 min read
Return all dates between two dates in an array in PHP
Returning all dates between two dates in an array means generating a list of all consecutive dates from the start date to the end date, inclusive, and storing each date as an element in an array for easy access. Here we have some common methods: Table of Content Using DatePeriod ClassUsing strtotime() FunctionUsing DatePeriod ClassUsing a While Loo
5 min read
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 isSame to compare dates Run the below command before ru
3 min read
How to compare two JavaScript array objects using jQuery/JavaScript ?
In this article, we are given two JavaScript array/array objects and the task is to compare the equality of both array objects. These are the methods to compare two JavaScript array objects: Using jQuery not() methodUse the sort() functionUse JSON.stringify() functionUsing every() and indexOf()Using Lodash _.isEqual() MethodApproach 1: Using jQuery
3 min read
JavaScript Program to Compare Two Strings Lexicographically
This JavaScript program compares two strings lexicographically, meaning it checks if one string comes before or after the other in alphabetical order. Lexicographical comparison is based on the Unicode value of each character in the strings. The program should return a positive value if the first string comes after the second string, a negative val
2 min read
How to compare two objects to determine the first object contains equivalent property values to the second object in JavaScript ?
In this article, we are going to learn about comparing two objects to determine if the first object contains equivalent property values to the second object, In JavaScript, comparing the values of two objects involves checking if they have the same properties with corresponding values. Given two objects obj1 and obj2 and the task are to check that
6 min read
JavaScript Program to Compare Two Strings
In JavaScript, comparing two strings can be essential for performing tasks like user authentication. It can involve checking if the user has entered the same credentials as there are in the database. This article provides insights into comparing two strings in JavaScript. It explores four distinct approaches with detailed explanations and examples.
3 min read
How to compare two arrays in JavaScript ?
We will see how to compare two arrays in JavaScript. First, we need to compare whether the length of both arrays should be the same or not, and then whether objects present in them are of the same type and whether each item of one array is equal to the other array or not. These are the following approaches to compare two arrays in JavaScript: Table
6 min read
How to calculate minutes between two dates in JavaScript ?
Given two dates and the task is to get the number of minutes between them using JavaScript. Approach: Initialize both Date object.Subtract the older date from the new date. It will give the number of milliseconds from 1 January 1970.Convert milliseconds to minutes. Example 1: This example uses the current date and the next new year date to get the
2 min read
How to check if one date is between two dates in JavaScript ?
The task is to determine if the given date is in between the given 2 dates or not? Here are a few of the most used techniques discussed with the help of JavaScript. In the first approach, we will use .split() method and the new Date() constructor. And in the second approach we will use the .getTime() method with the new Date() constructor. Approach
3 min read
How to calculate the number of days between two dates in JavaScript ?
To calculate the number of days between two dates in JavaScript, we use the Date object and perform simple arithmetic using milliseconds. Below are the methods commonly used to determine the number of days between two dates: Table of Content Using Two different dates Using One fixed date Using UTC timestamps Using Intl.DateTimeFormat Method JavaScr
4 min read
How to Compare Two Objects using Lodash?
To compare two objects using lodash, we employ Lodash functions such as _.isEqual(), _.isMatch(), and _.isEqualWith(). These methods enable us to do a comparison between two objects and determine if they are equivalent or not. Below are the approaches to do a comparison between two objects using Lodash: Table of Content Using Lodash _.isEqual() Met
4 min read
How to compare two images using Image comparison slider?
In this project, we are going to create an image slider with which we can check 2 images. If we are making an exact copy of them. We can compare every single boundary of the first image with the second image in both the vertical and horizontal directions. Approach: Create an HTML file in which we are going to add the main div, further we are adding
3 min read
How to get Difference between two Dates in days using jQuery ?
In this article, we will learn how to find the difference between the 2 dates in days using jQuery. Here, we will take the input for the dates using a jquery datepicker. Considering the given below 2 input Dates, we need to calculate the difference between them which will be returned in a number of days. Input : 02/17/2022 02/10/2022Output: 7For th
3 min read
How to select Min/Max dates in an array using JavaScript ?
Given an array of JavaScript date. The task is to get the minimum and maximum date of the array using JavaScript. Below are the following approaches: Table of Content Using Math.max.apply() and Math.min.apply() MethodsUsing reduce() methodUsing Spread OperatorUsing Array.prototype.sort with a custom comparatorApproach 1: Using Math.max.apply() and
3 min read
What is the best way to compare two HTML pages with same data but different markup?
In this article, we will be learning about the best way to compare two HTML pages with the same data but different markup. At first, we need to know how to compare two HTML pages. Now, the most efficient way to check both the pages is to use the hash code of both the page contents and compare it for equality. JavaScript hashCode(): In order to impl
4 min read
How to Compare Files From Two Different Branches in Git?
To compare files from two different branches in Git, you can use the git diff command. This command allows you to view the differences between two branches or commits. Below are the steps to compare files from two different branches: Table of Content Using Git DiffUsing Git DifftoolCheckout the BranchesRun the Diff CommandView the DifferencesOption
3 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.DateTimeFormatUsing a Custom Date Comparison Func
4 min read
How to Compare Two Values from Database and Post in PHP?
When working with databases in PHP, comparing two values and posting the result involves querying the database to retrieve the values, performing a comparison operation, and then using the results as needed. This process typically includes connecting to the database, executing a query to fetch the values, comparing them using PHP logic, and posting
4 min read
PHP | Number of week days between two dates
You are given two string (dd-mm-yyyy) representing two date, you have to find number of all weekdays present in between given dates.(both inclusive) Examples: Input : startDate = "01-01-2018" endDate = "01-03-2018" Output : Array ( [Monday] =&gt; 9 [Tuesday] =&gt; 9 [Wednesday] =&gt; 9 [Thursday] =&gt; 9 [Friday] =&gt; 8 [Saturday] =&gt; 8 [Sunday]
2 min read
How to Calculate the Number of Days Between two Dates in TypeScript ?
In TypeScript, calculating the number of days between two dates is a common task in many applications. Whether you're working with date ranges, scheduling, or performing date-based calculations, knowing how to accurately determine the difference in days between two dates is essential. Different approaches to achieve this in TypeScript: Table of Con
3 min read
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 i momentThese are the following approaches we are
2 min read
Program to find the number of days between two dates in PHP
In this article, we will see how to get the date difference in the number of days in PHP, along with will also understand the various ways to get the total count of difference in 2 dates &amp; see their implementation through the examples. We have given two dates &amp; our task is to find the number of days between these given dates. For this, we w
3 min read
How to compare isNaN() and isInteger() Methods in JavaScript ?
JavaScript provides various in-built functions that developers can use to check the type of values of variables. Two such functions are isNaN() and isInteger(). In this article, we will discuss the difference between these two functions and when to use them. isNaN() Method: The JavaScript isNaN() method is used to check whether a given value is an
2 min read
Optimum way to compare strings in JavaScript
In this article, we will know the optimal way to compare the strings using built-in JavaScript methods &amp; will see their implementation through the examples. The question is to compare 2 JavaScript strings optimally. To do so, here are a few of the most used techniques discussed. The method discussed below is used in the following examples. Stri
3 min read
How to compare date part only without comparing time in JavaScript?
To obtain date and time in javascript, we have the Date() class. We access Date() class by creating its objects. Date() class returns date and time both combined. There are four ways of instantiating a date: Syntax: var d = new Date(); //returns the present date and time or var d = new Date(milliseconds); //to set a specific date or var d = new Dat
4 min read
JavaScript Intl Collator compare() Method
This intl.collator.prototype.compare() method basically used to make the comparison between two strings as per the sorting order of the collator object. Here the compare-getter function gives a number as first-string and second-string compared between two strings as per the sorting order of the collator object, It shows a value less than 0 if the f
2 min read
How to Compare Objects in JavaScript?
Comparing objects is not as simple as comparing numbers or strings. Objects are compared based on their memory references, so even if two objects have the same properties and values, they are considered distinct if they are stored in different memory locations. Below are the various approaches to compare objects in JavaScript: Table of Content Equa
3 min read
Compare the Case Insensitive strings in JavaScript
Comparing strings in a case-insensitive manner means comparing them without taking care of the uppercase and lowercase letters. Here are some common approaches to compare the case-insensitive string in JavaScript: Table of Content Approach 1: JavaScript toUpperCase() functionApproach 2: JavaScript toLowerCase() functionApproach 3: Using localCompar
3 min read
How to Compare Objects for Equality in JavaScript?
When working with JavaScript it is common to compare objects for equality. we will explore various approaches to compare objects for equality in JavaScript. These are the following approaches: Table of Content Using JSON.stringifyUsing Lodash's isEqual() MethodUsing Object.keys() and Array.every()Using Object.entries() and Array.every()Using JSON.s
3 min read
Article Tags :
three90RightbarBannerImg