如何仅使用以下代码段来比较日期
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&lt; now);它不应该。我认为这是由于时间问题。
我们如何比较日期(而不是时间)?
答案 0 :(得分: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
}
}