Posted inJavaScript / jQuery

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 infinityknow.com.
Your valuable feedback, question, or comments about this Article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

Your email address will not be published. Required fields are marked *

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype