34

I have two variables namely

date1 = Mon Nov 25 2013 00:00:00 GMT+0530 (IST)
date2 = Mon Nov 25 2013 14:13:55 GMT+0530 (IST)

When I compare the two dates I get that date2 is greater which I need is correct. But I do not want to check the time part of the two dates I have. How could I get the date part alone from these two dates and compare it?

var today = new Date();     //Mon Nov 25 2013 14:13:55 GMT+0530 (IST) 
d = new Date(my_value);     //Mon Nov 25 2013 00:00:00 GMT+0530 (IST) 
if(d>=today){               //I need to check the date parts alone.
    alert(d is greater than or equal to current date);
}
Luke Girvin
  • 12,913
  • 8
  • 60
  • 81
Outlooker
  • 3,894
  • 2
  • 19
  • 37
  • 5
    possible duplicate of [Comparing date part only without comparing time in javascript](http://stackoverflow.com/questions/2698725/comparing-date-part-only-without-comparing-time-in-javascript) – Geeky Guy Nov 25 '13 at 09:19

4 Answers4

63

Try clearing the time using Date.setHours:

dateObj.setHours(hoursValue[, minutesValue[, secondsValue[, msValue]]])

Example Code:

var today = new Date();
today.setHours(0, 0, 0, 0);
d = new Date(my_value); 
d.setHours(0, 0, 0, 0);

if(d >= today){ 
    alert(d is greater than or equal to current date);
}
KyleMit
  • 35,223
  • 60
  • 418
  • 600
Praveen
  • 53,079
  • 32
  • 129
  • 156
  • 3
    @Outlooker *FYI* [you can't use == for date comparison](http://stackoverflow.com/questions/20162435/javascript-compare-two-dates-to-get-a-difference/20162708#comment30055600_20162603). Just to let you know. – Praveen Nov 25 '13 at 09:51
  • is right. Try comparing like this `d.getTime()==today.getTime()` – Ashique razak Oct 07 '21 at 09:11
13

The best way would be to modify the accepted answer's if statement as follows

if(d.setHours(0,0,0,0) >= today.setHours(0,0,0,0))

In this way, you can easily check for equality as well because the return type for setHours() is integer.

Saksham
  • 8,705
  • 6
  • 42
  • 67
6

Try:

    var today = new Date();     //Mon Nov 25 2013 14:13:55 GMT+0530 (IST) 
    var d = new Date(my_value);     //Mon Nov 25 2013 00:00:00 GMT+0530 (IST) 
    var todayDateOnly = new Date(today.getFullYear(),today.getMonth(),today.getDate()); //This will write a Date with time set to 00:00:00 so you kind of have date only
    var dDateOnly = new Date(d.getFullYear(),d.getMonth(),d.getDate());

    if(dDateOnly>=todayDateOnly){               
        alert(d is greater than or equal to current date);
    }
Igle
  • 4,572
  • 4
  • 30
  • 64
-1
     var StartDate = $("#StartDate").val();

      var EndDate = $("#EndDate").val();

      if ((( EndDate - StartDate)/ (86400000*7))<0) 
      { 
       alert("Start Date Must Be Earlier Than End Date"); $("#StartDate").focus(); 
        error = true; 
        return false; 
      }
Bloodhound
  • 2,836
  • 10
  • 33
  • 69
  • 1
    Welcome to SO! Please realize this question is more than 2 years old… You can still answer of course, especially when techniques evolve, new tools / languages are available, which open room for better solutions. In that case, please kindly explain why you think your solution is better than the previously proposed ones. – ghybs Jan 05 '16 at 12:35