Thursday, July 4, 2024
HomeLanguagesJavascriptHow to compare date part only without comparing time in JavaScript?

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 Date(dateString);
//to set a specific date

or

var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);
//to set a specific date:

So the Date() returns function Date() { [native code] } and we can turn that into a string by using object.toString()

Approach 1:
When the objects get compared it gets compared both with time and date.
So now we gonna make the time to 00:00:00(stop) in that way when the objects get compared only the date gets compared as the time for both date objects will be 00:00:00(stop).

To set the time as 00:00:00(stop), setHours() comes in handy.

Syntax;

//the hour(0-23) is mandatory.
dateobject.setHours(hour, min, sec, millisec)

And for making the time as 0 or completely stopped, we have to make sure that all the parameters are 0 including milli-seconds.

Example:




<!DOCTYPE html>
<html>
  
<body>
  
    <p>Below date1 and data2 are compared for older, further, and same dates.</p>
  
    <script>
        //Example-1
        var date1 = new Date();
        date1.setHours(0, 0, 0, 0)
        var date2 = new Date(2020, 8, 20);
        //stops the clock
        date2.setHours(0, 0, 0, 0)
        document.write("date1=>", date1);
        document.write("<br \>date2=>", date2);
        if (date1 > date2) {
            document.write("<br \>date1 is further than date2");
        } else if (date1 < date2) {
            document.write("<br \>date1 is older than date2")
        } else {
            document.write("<br \>date1 and date2 same")
        }
        //Example-2
        date2 = new Date(2019, 11, 3);
        document.write("<br \><br \>date1=>", date1);
        document.write("<br \>date2=>", date2);
        if (date1 > date2) {
            document.write("<br \>date1 is further than date2");
        } else if (date1 < date2) {
            document.write("<br \>date1 is older than date2")
        } else {
            document.write("<br \>date1 and date2 same")
        }
        //Example-3
        date1 = new Date();
        date2 = new Date();
        date1.setHours(0, 0, 0, 0)
        date2.setHours(0, 0, 0, 0)
        document.write("<br \><br \>date1=>", date1);
        document.write("<br \>date2=>", date2);
        if (date1 > date2) {
            document.write("<br \>date1 is further than date2");
        } else if (date1 < date2) {
            document.write("<br \>date1 is older than date2")
        } else {
            document.write("<br \>date1 and date2 same.")
        }
        //Example-4
        date1 = new Date();
        date2 = new Date();
        date1.setHours(0, 0, 0, 0)
            /*Due to, time didn't get stop in data2,
            it’s time also gets compared to data1.*/
            //date2.setHours(0,0,0,0)
        document.write("<br \><br \>date1=>", date1);
        document.write("<br \>date2=>", date2);
        if (date1 > date2) {
            document.write("<br \>date1 is further than date2");
        } else if (date1 < date2) {
            document.write("<br \>date1 is older than date2")
        } else {
            document.write("<br \>date1 and date2 same.")
        }
    </script>
  
</body>
  
</html>


Output

Below date1 and data2 are compared for older, further, and same dates.

date1=>Wed Dec 04 2019 00:00:00 GMT+0530 (India Standard Time)
date2=>Sun Sep 20 2020 00:00:00 GMT+0530 (India Standard Time)
date1 is older than date2

date1=>Wed Dec 04 2019 00:00:00 GMT+0530 (India Standard Time)
date2=>Tue Dec 03 2019 00:00:00 GMT+0530 (India Standard Time)
date1 is further than date2

date1=>Wed Dec 04 2019 00:00:00 GMT+0530 (India Standard Time)
date2=>Wed Dec 04 2019 00:00:00 GMT+0530 (India Standard Time)
date1 and date2 same.

date1=>Wed Dec 04 2019 00:00:00 GMT+0530 (India Standard Time)
date2=>Wed Dec 04 2019 05:37:26 GMT+0530 (India Standard Time)
date1 is older than date2

Approach 2:
Here we gonna use toDateString() to obtain the only date, but when obtaining it gets converted to a string, as the comparison is not possible for strings, we need to get then again converted to a date object, now when converting them back to object time sets to 00:00:00(stops).

Syntax:

var date1 = new Date(new Date().toDateString());

Breakdown of the above syntax

new Date().toDateString()
converts the Date object to a string containing only the date part.
new Date(new Date().toDateString()); 
converts it back to Date object setting time to 00:00:00

Example




<!DOCTYPE html>
<html>
  
<body>
  
    <p>Below date1 and data2 are compared for older,
      further, and same dates.</p>
  
    <script>
        //Example-1
        var date1 = new Date(new Date().toDateString());
        var date2 = new Date(new Date(2018, 8, 20).toDateString());
        //var date2 = new Date(2018,8,20).toDateString();
        document.write("date1=>", date1); //returns only the date.
        document.write("<br \>date2=>", date2);
        if (date1 > date2) {
            document.write("<br \>date1 is further than date2");
        } else if (date1 < date2) {
            document.write("<br \>date1 is older than date2")
        } else {
            document.write("<br \>date1 and date2 same")
        }
    </script>
</body>
  
</html>


Output

Below date1 and data2 are compared for older, further, and same dates.

date1=>Fri Dec 20 2019 00:00:00 GMT+0530 (India Standard Time)
date2=>Thu Sep 20 2018 00:00:00 GMT+0530 (India Standard Time)
date1 is further than date2

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

Nicole Veronica Rubhabha
Nicole Veronica Rubhabha
A highly competent and organized individual DotNet developer with a track record of architecting and developing web client-server applications. Recognized as a personable, dedicated performer who demonstrates innovation, communication, and teamwork to ensure quality and timely project completion. Expertise in C#, ASP.Net, MVC, LINQ, EF 6, Web Services, SQL Server, MySql, Web development,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments