Kamlesh Bisht
Kamlesh Bisht

Reputation: 53

Parsing Date values

Right now my function is

    if(dateCheck("02.05.2013","17.09.2013","02.07.2013"))
    alert("Availed");
else
    alert("Not Availed");

function dateCheck(from,to,check) {

    var fDate,lDate,cDate;
    fDate = Date.parse(from);
    lDate = Date.parse(to);
    cDate = Date.parse(check);

    if((cDate <= lDate && cDate >= fDate)) {
        return true;
    }
    return false;
}

Always returning false, Getting dates in this format from the server side 02.09.2013 Please suggest right format for parsing these values?

Upvotes: 0

Views: 78

Answers (3)

Ataur Rahman Munna
Ataur Rahman Munna

Reputation: 3917

You should parse your date to date object in javascript. This link can give you an idea of date constructor in javascript.It has a constructor new Date(year,month,date). so parse your dateString in that format. follow the code

if(dateCheck("02.05.2013","02.09.2013","02.07.2013"))
        alert("Availed");
    else
        alert("Not Availed");
    
    function dateCheck(from,to,check) {
    
        var fDate,lDate,cDate;
        fDate = new Date(from.split(".")[2],from.split(".")[1],from.split(".")[0]);
        
        lDate = new Date(to.split(".")[2],to.split(".")[1],to.split(".")[0]);
        cDate = new Date(check.split(".")[2],check.split(".")[1],check.split(".")[0]);
    	
    
    
        if((cDate<= lDate) && (cDate >= fDate)) {
            return true;
        }
        return false;
    }

See the jsFiddle

Upvotes: 1

Sadhu
Sadhu

Reputation: 890

if(dateCheck("02/05/2013","02/09/2013","02/06/2013"))
    alert("Availed");
else
    alert("Not Availed");

function dateCheck(from,to,check) {

    var fDate,lDate,cDate;

    fDate = convertDate(from);
    lDate = convertDate(to);
    cDate = convertDate(check);

    if((cDate <= lDate && cDate >= fDate)) {
        return true;
    }
    return false;
}


function convertDate(dateStr) {
    var parts = dateStr.split("/");
    return new Date(parts[2], parts[0], parts[1] - 1);
}

Upvotes: 0

eX0du5
eX0du5

Reputation: 896

Looks like duplicate of Why does Date.parse give incorrect results?. Your date is not clear as 02.09. might be the second of September or the 9th of Feb. See the referenced post for details.

Upvotes: 1

Related Questions