How to compare the date part alone from a date time value

后端 未结 4 2051
栀梦
栀梦 2020-12-15 16:21

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

相关标签:
4条回答
  • 2020-12-15 16:25

    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.

    0 讨论(0)
  • 2020-12-15 16:31

    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);
    }
    
    0 讨论(0)
  • 2020-12-15 16:40
         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; 
          }
    
    0 讨论(0)
  • 2020-12-15 16:44

    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);
        }
    
    0 讨论(0)
提交回复
热议问题