3

我是 php 和 javascript 的新手。这就是让我陷入困境的原因 政府给出了两个日期。

var a="24/05/2013";
var b="26/05/2013";

假设用户选择签入日期为:17/05/2013并签出日期为:30/05/2013。如您所见,这些选定的日期介于上述日期之间(var a 和 var b)。那么我如何使用 JAVASCRIPT 验证该场景。

在这方面需要支持。

提前致谢

4

3 回答 3

0

尝试这个 :-

 var fdt= new Date("20/02/2013");
    var tdt = new Date("10/05/2013");

function validateFromAndToDate(fdt,tdt){

    var dt1  = dte1.value.substring(0,2); 
    var mon1 = dte1.value.substring(3,5); 
    var yr1  =dte1.value.substring(6,10); 

    var dt2  = dte2.value.substring(0,2); 
    var mon2 = dte2.value.substring(3,5); 
    var yr2  = dte2.value.substring(6,10); 
    var date1 = new Date(yr1, mon1-1, dt1); 
    var date2 = new Date(yr2, mon2-1, dt2); 

        if (date2<date1){
            alert("Date period must be within the given date!");
            return false
        }
    return true
 }

它工作正常。

于 2013-05-10T07:36:38.790 回答
0

我会这样做:

function dateCheck() {
    var a = new Date("24/05/2013");
    var b = new Date("26/05/2013");
    var checkinDate = Date.parse(document.getElementById("checkinDate").value);
    var checkoutDate = Date.parse(document.getElementById("checkoutDate").value);

    return((checkinDate >= a && checkinDate <= b) && 
       (checkoutDate <= b && checkoutDate >= a) && 
       (checkoutDate > checkinDate))            
}

编辑:根据 OP 的说明

function dateCheck() {
        var a = new Date("24/05/2013");
        var b = new Date("26/05/2013");
        var checkinDate = Date.parse(document.getElementById("checkinDate").value);
        var checkoutDate = Date.parse(document.getElementById("checkoutDate").value);

        return(( a > checkinDate && a < checkoutDate) && 
           (b < checkoutDate && b > checkinDate) && 
           (checkoutDate > checkinDate))            
    }
于 2013-05-10T06:54:05.863 回答
0

尝试这个

function dateCheck() {
    var fDate = new Date("24/05/2013");
    var lDate; = new Date("26/05/2013");
    fDate = Date.parse(document.getElementById("fDate").value);
    lDate = Date.parse(document.getElementById("lDate").value);

    if(fDate <= lDate) {
        alert("true");
        return true;
    }
    alert("false");
    return false;
}
于 2013-05-10T06:22:44.823 回答