JavaScript日期会抛出无效日期

时间:2013-12-05 06:48:14

标签: javascript validation date undefined

我正在尝试比较两个日期from date and to date以下是我的方法:

function isValidDate() {
        var fromDate ="";
        var toDate ="";

        var fromDateTemp = $("#fromRequestDate").val(); //2013-12-05
        var toDateTemp = $("#toRequestDate").val(); //2013-12-01

        if(fromDateTemp.length != '0' && toDateTemp.length != '0'){
            fromDate = new Date(fromDateTemp);
            toDate = new Date(toDateTemp);
        }

        if (fromDate.length != '0' || toDate.length != '0') {
            $("#validationMessage").text("Please Select From Date and To Date");
            return false;

        } else if (fromDate.getDate() > toDate.getDate()) {
            $("#validationMessage").text("From Date is greater than To Date");
            return false;

        } else {
            $("#validationMessage").text("");
            return true;

        }
    }

但在计算长度时我得到Ivalid Date以及undefined

所以请帮我找到问题。

由于

5 个答案:

答案 0 :(得分:2)

我们通常使用getTime比较两个日期(它返回自1970年以来经过的毫秒数)

所以,

fromDate.getTime() > toDate.getTime()

应该工作。当然,假设从#fromReqDate和#toReqDate获取的值是Date的实例(简单检查fromDate instanceOf Date应该返回true

答案 1 :(得分:2)

我认为错误是在您的情况下检查if (fromDate.length == '0' || toDate.length == '0')而不是您使用if(fromDateTemp.length != '0' && toDateTemp.length != '0')检查代码并在您面临问题时还原。

var fromDate ="";
var toDate ="";

var fromDateTemp = '2013-12-05'; //2013-12-05
var toDateTemp = '2013-12-01'; //2013-12-01

if(fromDateTemp.length != '0' && toDateTemp.length != '0'){
    fromDate = new Date(fromDateTemp);
    toDate = new Date(toDateTemp);
}

if (fromDate.length == '0' || toDate.length == '0') {
   alert("Please Select From Date and To Date");
    return false;

} else if (fromDate.getDate() > toDate.getDate()) {
    alert("From Date is greater than To Date");
    return false;

} else {
    alert("");
    return true;

}

答案 2 :(得分:2)

尝试使用它。

if(isNaN(new Date(fromDateTemp)))
{
    alert("Please enter a valid from date");
    return;
}

答案 3 :(得分:2)

var fromDateTemp = $("#fromRequestDate").val(); //2013-12-05
var toDateTemp = $("#toRequestDate").val(); //2013-12-01

而不是这两个代码只需替换以下代码

var fromDateTemp = new Date(''+$("#fromRequestDate").val()); //2013-12-05
var toDateTemp = new Date(''+$("#toRequestDate").val()); //2013-12-01

答案 4 :(得分:1)

这是纠正过的js:

Demo

function isValidDate() {
        var fromDate = null;
        var toDate = null;

        var fromDateTemp = $("#fromRequestDate").val();
        var toDateTemp = $("#toRequestDate").val();

        if(fromDateTemp.length != 0 && toDateTemp.length != 0){
            fromDate = new Date(fromDateTemp);
            toDate = new Date(toDateTemp);
        }

        if (fromDate == null || toDate == null) {
            $("#validationMessage").text("Please Select From Date and To Date");
            return false;

        } else if (fromDate.getTime() > toDate.getTime()) {
            $("#validationMessage").text("From Date is greater than To Date");
            return false;

        } else {
            $("#validationMessage").text("");
            return true;

        }
    }