2015-09-01 30 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