简体   繁体   English

使用jQuery进行日期比较

[英]Date comparison using jquery

I use the following code to check the date is before today! 我使用以下代码检查日期是否在今天之前! But when I select today, the condition becomes true! 但是当我今天选择时,条件变为真!

var fromDate = $('#from').datepicker('getDate');
var toDate = $('#to').datepicker('getDate');
var today = new Date();

if(today >= fromDate || toDate <= today){
    alert('Cannot book dates prior to today.');
    $('#from, #to').val('')
    return false;
}

What am I doing wrong here? 我在这里做错了什么?

Should be something like 应该是这样的

var fromDate = $('#from').datepicker('getDate');
var toDate = $('#to').datepicker('getDate');
var today = new Date();
 today.setHours(0,0,0,0);
fromDate.setHours(0,0,0,0);
toDate.setHours(0,0,0,0);

if(today > fromDate || toDate < today){
    alert('Cannot book dates prior to today.');
    $('#from, #to').val('')
    return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM