javascript compare dates – How to compare two dates in JavaScript?

javascript compare dates – The ==, !=, ===, and !== operators require you to use date.getTime() as in Example

var startdate = new Date();
var enddate = new Date(startdate);
var same = startdate.getTime() === enddate.getTime();
var notSame = startdate.getTime() !== enddate.getTime();

javascript compare dates

there are different methods by which we can compare dates, like as: Comparing two dates with one another, Comparing date with time and Comparing dates using getTime() Examples.

Comparing two dates with one another

 Comparing Dates

Comparing date with time

 Comparing Date and time

Example2: Comparing same dates with disimilar timings

 Comparing same date but different time

Compare two dates with JavaScript

javascript compare dates Function

function dateCompare(d1, d2){
    const startdate = new Date(d1);
    const enddate = new Date(d2);

    if(startdate > enddate){
        console.log(`${d1} is greater than ${d2}`)
    } else if(startdate < enddate){
        console.log(`${d2} is greater than ${d1}`)
    } else{
        console.log(`Both dates are equal`)

dateCompare("6/11/2022", "7/8/2019")
dateCompare("01/01/2023", "01/01/2023")

Don't Miss : How To Get Difference Between Two Dates In Javascript?

I hope you get an idea about javascript compare dates.
I would like to have feedback on my
Your valuable feedback, question, or comments about this Article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment