2015-09-01 100 views
1

如何只能用下面的片段来比较日期如何使用datepicker JQuery比较“仅日期;不是时间”?

var selectedDate = $("input[id='accountDate']").datepicker('getDate'); 

     if (selectedDate != null) { 
      var now = new Date(); 
      console.log("selectedDate:" + selectedDate + " | now: " + now) 

      if (selectedDate < now) { 
       do this and this and this 
       } 
      } 
     } 

日志输出:

selectedDate (Entered Date):Tue Sep 01 2015 00:00:00 GMT+0200 (W. Europe Standard Time) 
| now: Tue Sep 01 2015 09:43:33 GMT+0200 (W. Europe Standard Time) 

正如你在输出日期看到的是相同的还是它正在执行(selectedDate <现在);它不应该。我认为这是由于时间问题。

我们如何比较日期(不是时间)?

回答

2

您可以现在一部分时间设置为空白像

var selectedDate = $("input[id='accountDate']").datepicker('getDate'); 

if (selectedDate != null) { 
    var now = new Date(); 
    now.setHours(0, 0, 0, 0) 
    console.log("selectedDate:" + selectedDate + " | now: " + now) 

    if (selectedDate < now) { 
     do this and this and this 
    } 
} 
+0

感谢。让我尝试 – fatherazrael