24

I am trying this but is not working... why?

<html>
<body>
    <script type="text/javascript">

        var today=new Date(); //today is Nov 28, 2010
        today.setHours(0);
        today.setMinutes(0);
        today.setSeconds(0);
        document.write(today+" ");

        var today2 = new Date("November 28, 2010");
        document.write(today2 + " ");
        if (today == today2) { document.write("==");
        if (!(today > today2) && !(today < today2) ) {document.write("==  ");}
        if (today > today2) { document.write(">  ");}
        if (today >= today2 ){ document.write(">=  ");}
        if (today < today2 ) { document.write("<  ");}
        if (today <= today2 ){ document.write("<=  ");}

    </script>
</body>
</html>

And I always get this:

Sun Nov 28 2010 00:00:00 GMT+0900 (JST) Sun Nov 28 2010 00:00:00 GMT+0900 (JST) > >=

Aren't both dates to be the same? Hence, I should get == printed but is not happening... ;(

Thank you for your help in advance.

Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
nacho4d
  • 43,720
  • 45
  • 157
  • 240
  • Did you ask for a way to compute the date difference or a way to determine if a given date is today? For the first question, see: http://stackoverflow.com/questions/41948/how-do-i-get-the-difference-between-two-dates-in-javascript. For the second, check my answer. – KooiInc Nov 27 '10 at 19:30
  • Thank you all for the answers... this was kind of a js experiment ;) – nacho4d Nov 28 '10 at 04:49

3 Answers3

71

They will never match because you're comparing two separate Date object instances.

You need to get some common value that can be compared. For example .toDateString().

today.toDateString() == today2.toDateString();  // true

If you just compare two separate Date objects, even if they have the exact same date value, they are still different.

For example:

today == new Date( today );  // false

They are the same date/time value, but are not the same object, so the result is false.

Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
user113716
  • 318,772
  • 63
  • 451
  • 440
22
function today(td) {
    var d = new Date();
    return td.getDate() == d.getDate() && td.getMonth() == d.getMonth() && td.getFullYear() == d.getFullYear();
}
Kalamarico
  • 5,466
  • 22
  • 53
  • 70
user2619282
  • 221
  • 2
  • 2
  • This solution is faster than today.toDateString() == today2.toDateString(); Performance test result at jsbench.me shows that comparing strings is 99% slower. At http://jsben.ch/ it is 80% slower. – carlosvin Aug 05 '19 at 13:49
0

You can do it by setting time to 00:00:00 for the given date and for today's date, and then compare them.

The code example is below:

function checkDateIsToday (today, givenDate) {
  
  if(today.setHours(0,0,0,0) === givenDate.setHours(0,0,0,0)) {
     console.log('Date is todays date');
  } else {
     console.log('Date is not todays date');
  } 

}

checkDateIsToday(new Date(), new Date('2020-07-10'));
Kate Orlova
  • 3,225
  • 5
  • 11
  • 35
Tanzeem Bhatti
  • 219
  • 2
  • 3